Calculating a fractional value.

This is a discussion on Calculating a fractional value. within the C++ Programming forums, part of the General Programming Boards category; Hi, just wondering if there is away of telling if a number is fractional or not. I have a double ...

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

    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
    359
    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,652
    Code:
    bool is_fractional( double n ) {
      return ( (n - fabs(n)) != 0);
     }
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  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, 08:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21