Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 29th, 2012, 01:47 PM
 KevinFCTwente
Registered User

Join Date: Sep 2012
Posts: 2
Time spent in forums: 1 h 20 m 42 sec
Reputation Power: 0
Whole number check

Is there an easy way to check if the result of a calculation (bv 10 / 9 or 10 / 5) is a whole number or not?

#2
October 29th, 2012, 02:01 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,187
Time spent in forums: 1 Month 3 Weeks 2 Days 13 h 13 m 26 sec
Reputation Power: 455
Code:
if (fmod((double)numerator,(double)denominator))
fputs("NOT ",stdout);
puts("whole");
__________________
[code]Code tags[/code] are essential for python code!

#3
October 29th, 2012, 02:44 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
Quote:
 Originally Posted by KevinFCTwente Is there an easy way to check if the result of a calculation (bv 10 / 9 or 10 / 5) is a whole number or not?

The result of integer division (as in your exemple) (except division by zero) is ALWAYS an integer.
The result of floating-point division (except division by zero and overflows) is always a floating-point number.

If you want to determine if a floating point number has the same value as a whole number, that's another problem:
for instance 4.2E-289 is not a whole number (zero?) but 4.2E289 is a whole number.

#4
October 29th, 2012, 02:50 PM
 TheOtherDino
Contributing User

Join Date: Jan 2010
Location: Katy, Texas
Posts: 495
Time spent in forums: 4 Days 20 h 3 m 19 sec
Reputation Power: 198
In simple math...

if (numerator == (numerator/denominator)*denominator) then the result of numerator/denominator is a whole number ;
else it isn't ;
__________________
Do you agree? Disagree? And remember, it's all about the reputation power...

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Whole number check