#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Code:
    if (fmod((double)numerator,(double)denominator))
      fputs("NOT ",stdout);
    puts("whole");
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    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.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    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...

IMN logo majestic logo threadwatch logo seochat tools logo