Thread: Calculating a fractional value.

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Question Calculating a fractional value.

    Hi, just wondering if there is away of telling if a number is fractional or not. I have a double variable which is being divided several times, the result of which needs to be known whether it is whole or fractional. You can’t use the % mod operator on floating point numbers.

    The most likely way would be to do something like this:

    Code:
    unsigned long Whole;
    double Temp = 1789664465465, Result;
    
    Result = Temp / 2;
    Whole = Temp //truncate value
    
    if(Whole – Result == 0) //Result must be a whole number
    {
      //do whatever
    }
    The only trouble with this method is the unsigned long can only store unsigned numbers up to 4,294,967,295, therefore assigning Result to Whole would be out of range. Can anyone help us with this? There must be a way to find out if Result is fractional or not.
    Be a leader and not a follower.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look up fmod() in <cmath>
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Excellent, theres a long double version fmodl(),
    cheers mate.
    Be a leader and not a follower.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    bool is_fractional( double n ) {
      return ( (n - fabs(n)) != 0);
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You have to be careful and compare it's
    between (epsilon, -epsilon).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  2. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 09:17 PM