'<=' operator not working properly?

This is a discussion on '<=' operator not working properly? within the C++ Programming forums, part of the General Programming Boards category; I wrote a program that, among other things, utilizes a simple while loop with a '<=' operator. It malfunctioned in ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    Question '<=' operator not working properly?

    I wrote a program that, among other things, utilizes a simple while loop with a '<=' operator. It malfunctioned in that it ignored the 'or equal to' part of the operator and quit the loop early. Here is an example of the part of the program that is malfunctioning, along with the output.

    Code:
    #include <iostream>
    using namespace std;
    main(){
      double d = 0.1;
      double e = 0.02;
      double f = 0.2;
      while( d <= f ){
        cout << "d: " << d << endl;
        d += e;
      }
      cout << endl;
      double a = 0.3;
      double b = 0.05;
      double c = 0.7;
      while( a <= c ){
        cout << "a: " << a << endl;
        a += b;
      }
    }
    This program should first loop from 0.1 to 0.2 in increments of 0.02, printing every number from 0.1 to 0.2, inclusive. It then should loop from 0.3 to 0.7 in increments of 0.05, printing every number from 0.3 to 0.7, inclusive. Here is what it prints:

    d: 0.1
    d: 0.12
    d: 0.14
    d: 0.16
    d: 0.18
    d: 0.2

    a: 0.3
    a: 0.35
    a: 0.4
    a: 0.45
    a: 0.5
    a: 0.55
    a: 0.6
    a: 0.65

    Notice that it does not print 0.7. This is my problem. The same program, executing the same loop, is behaving differently when using different values in the loop. Am I missing something blatantly obvious here? This isn't a complex piece of code here, but maybe one of you out there knows what's happening when it claims that 0.7 <= 0.7 returns false. I'd appreciate any comments.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Floating point inaccuracies. Floating point numbers cannot represent all numbers with full accuracy. What happens is that when a reaches 0.7, it's actually slightly above - 0.70000324 or something like that. Thus, the comparison fails.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    const float tolerance = 0.00001;
    
    inline bool equal_to(float a, float b)
    {
           return (+(a - b) < tolerance);
    }
    Adjust the tolerance as needed, keeping in mind it introduces the possibility of precision loss.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    If you are stepping from 0.3 to 0.7 in steps of 0.05, then you will take (0.7 - 0.3) / 0.05 = 8 steps. So iterate over the (already known) number of steps, instead of comparing values.

    If you actually compute (0.7-0.3)/0.05, you'll get something like 7.99999, which you should obviously round to 8.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    4
    Thanks guys. I guess I shouldn't assume that any two floating point values are equal to the full precision of the computer.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by master5001
    +(a - b)
    Out of curiosity, why the plus operator?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    i thought the + should be an abs() ?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Yep, should be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  3. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  4. cout not working properly
    By Yoshi in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2002, 09:04 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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