Thread: Check to intiness

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Check to intiness

    This is no doubt simple, but how can I check if a double contains an integral value?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Test against INT_MIN and INT_MAX or 0 and UINT_MAX in <climits>, or the equivalent templates from numeric_limits in <limits>. But beware testing for equality with floating-point, it's a pain to get right.
    My best code is written with the delete key.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I've solved it: put the double's value into an int, then check equality of the original double with the new int typecasted as a double. If they are equal, then the original double is an integer. Gotta love truncation.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >put the double's value into an int
    And what if the double's value is out of range for an int? Signed overflow is undefined.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You could also have a "imperfect" floating point value.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I didn't compile this, but...basically this just ensures the value is in the range of an int, casts it, and then restores it to the original magnitude. The pointers are there to save us from having to use two separate loops (one for greater, one for less than zero).


    Code:
     double integral(double n)
    {
     int f = 0;
     double m, * l, * r;
    
         if(n > 0)
        {
         m = INT_MAX;
         l = &n, r = &m;
        }
         else
        {
         m = INT_MIN;
         l = &m, r = &n;
        }
    
         while(*l > *r)
        {
         ++f;
         n -= m;
        }
    
     n = (int)n;
     
         if(f)
        {
         n += f * m;
        }
    
     return n;
    }
    
    
     bool is_integral(double n)
    {
     return n-integral(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;
    }

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Neat. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM