infinite loop on floating point number

This is a discussion on infinite loop on floating point number within the C++ Programming forums, part of the General Programming Boards category; The following code is used to get the number of decimal places after the decimal point, but can go into ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    infinite loop on floating point number

    The following code is used to get the number of decimal places after the decimal point, but can go into an infinite loop sometimes on my compiler:
    g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)

    The questions are
    1) why does a double like 134.29 triggers an infinite loop? some floating point numbers cause such infinite loop, but most other floating point numbers just work fine. Are there any rules of thumb on the numbers which cause the infinite loop?
    2) baring using string length to get the number of decimal places, is there any other way to improve the existing code to avoid triggering the infinite loop?


    Code:
    #include <iostream>
    
    int main()
    {
        double data=134.29;
    
        double v = data;
        int8_t dpcount = 0;     
        static double EPS=1E-3;
    
        while(v - (int)v  > EPS)  
        {
            v *= 10;  
            ++dpcount;
            std::cout<< "data: " << data <<  " v: "<< v << " dpcount: " << (int) dpcount << std::endl;
        }           
    }

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,879
    You are seeing floating point inaccuracies. The value .29 can not be properly represented in binary so what this is actually something like: .28999999999999204. It also looks like your equation in your if statement is incorrect. I think you should be using the < operator instead of the > operator. This link seems to explain the issue very well.

    Jim

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Adding a
    Code:
    cout << int(v) << endl;
    to the loop, it looks like the actual value of data is just under what it should be, e.g. something like 134.289999999. The behavior of int(v) is to round down so the difference (v - int(v)) ends up being close to 1, rather than 0.

    A quick way to make int(v) round up in these cases is to add a small amount to v (and also use abs)
    Code:
    while (abs(v - int(v+.5)) > EPS)
    Consider this post signed

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Don't convert to int and then back to float just to get the non-integral portion of the value.
    Use the function called floor for that. Firstly it's much faster than converting both ways and also it still works when the value of the double exceeds that of an int.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 08:11 PM
  2. floating point number comparison
    By stanlvw in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2009, 02:44 PM
  3. Floating point number
    By Mckooy in forum C Programming
    Replies: 1
    Last Post: 01-15-2006, 07:28 AM
  4. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 09:14 PM
  5. greatest floating point number?
    By m712 in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2002, 07:29 AM

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