
September 13th, 2006, 07:34 PM
|
|
Contributing User
|
|
Join Date: Jan 2005
Posts: 600

Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
|
|
Since there is the possibility of coming up with non-whole numbers, I assume you are using floats or doubles. In that case, try casting the number to an int and see if the result is reasonably close. (I say reasonably close because floats are prone to round-off errors, i.e. you would probably consider 5.999999999999999999999 and 6.000000000000001 whole numbers, even though when rounded to ints they would compare inequal with the == operator. It will take some experimentation to see what you should define "reasonably close" as for this project.)
With that in mind, a function:
C Code:
Original
- C Code |
|
|
|
bool isWhole(float num, float epsilon) //could use doubles instead, of course { return (fabs(num - (int)num) <= epsilon); }
|