Advertisement

Is a float a whole number?

Started by January 07, 2001 06:20 PM
3 comments, last by gimp 24 years ago
How do I determine if a float is a whole number (ie has nothng after the decimal place)? I need to know if the result of my sqrt returns a whole number... Thanks Chris
Chris Brodie
A float is like a deciml. 10.43. It can be a whole number though.
Advertisement
Try this:

BOOL FloatIsWhole(float x){    return (x-(int)x)==0;}
I don''t think the last answer will always work the way you expect, as there can be precision errors with floats, in that you might do some integer arithmetic (such as sqrt(25)) and end up with 5.00000000000001 or whatever. So, instead of ==0, perhaps you could perhaps use <0.000001 && >-0.000001.

Anyway, what makes "return (x-(int)x)==0;" better than "return (x == (int)x)"?
How about

bool IsWholeSqrt( int startNum )
{
if( ( (int)sqrt( startNum )* (int)sqrt( startNum )) == startNum )
return true;
}


People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.

This topic is closed to new replies.

Advertisement