• Welcome to Valhalla Legends Archive.
 

[C++] Converting float to int

Started by sixb0nes, April 13, 2006, 06:45 PM

Previous topic - Next topic

sixb0nes

Hello. I've come across a rather interesting problem involving float to int conversions. If I have a float, say, 98.85, and I assign an int to this float times 100...

float myfloat = 98.85;
int myint = myfloat * 100;

myint becomes 9884, which is not what I want. Is there anyway to do this float->int conversion without losing precision and without making myint a float? I've figured a rather hack-like workaround by simply adding one to myint, but I would much rather a solution which did not involve this step.
Thanks.

rabbit

Floats are weird in C++.  Your '98.85' is actually '98.849999...'  Adding 0.01 and then multiplying should work.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Maddox

Quote from: sixb0nes on April 13, 2006, 06:45 PM
Hello. I've come across a rather interesting problem involving float to int conversions. If I have a float, say, 98.85, and I assign an int to this float times 100...

float myfloat = 98.85;
int myint = myfloat * 100;

myint becomes 9884, which is not what I want. Is there anyway to do this float->int conversion without losing precision and without making myint a float? I've figured a rather hack-like workaround by simply adding one to myint, but I would much rather a solution which did not involve this step.
Thanks.

Try this...

float myfloat = 98.85f;
int myint = (int)myfloat * 100;
asdf.

MyndFyre

Quote from: Maddox on April 15, 2006, 05:43 PM
Quote from: sixb0nes on April 13, 2006, 06:45 PM
Hello. I've come across a rather interesting problem involving float to int conversions. If I have a float, say, 98.85, and I assign an int to this float times 100...

float myfloat = 98.85;
int myint = myfloat * 100;

myint becomes 9884, which is not what I want. Is there anyway to do this float->int conversion without losing precision and without making myint a float? I've figured a rather hack-like workaround by simply adding one to myint, but I would much rather a solution which did not involve this step.
Thanks.

Try this...

float myfloat = 98.85f;
int myint = (int)myfloat * 100;


That would result in myint == 9800.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

I think he may have meant int myint = (int)(myfloat * 100); so that the multiplication would process before the casting.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

rabbit

It would automatically be cast to int because he is multiplying by an int and storing as an int.  A typecast is not needed.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.