Float vs Double if condition

I have a trivial question. It`s been on my table for 2 days now. I couldn`t solve it. I have three logical comparisons that is giving me weird results. I have added the part of the code that works and that doesn`t work. Can anyone help me in this regard. TIA.

1
2
3
4
5
6
7
8
9
10
11
    //If condition is not satisfied even though x is equal to -0.002
    float x = -0.002; 
    if(x >= -0.002){cout<<"Loops in"<<endl;}

    //If condition works if I change >= to <=
    float x = -0.002; 
    if(x <= -0.002){cout<<"Loops in"<<endl;}

     //If condition works for both <= and >= if I change the type of variable from float to double
    double x = -0.002; 
    if(x <= -0.002){cout<<"Loops in"<<endl;}
Last edited on
closed account (E3h7X9L8)
Due to rounding errors, most floating-point numbers end up being slightly imprecise. As long as this imprecision stays small, it can usually be ignored.However, it also means that numbers expected to be equal (e.g. when calculating the same result through different correct methods) often differ slightly, and a simple equality test fails.

For comparing floating-point numbers you could do the absolute difference between them and compare them to epsilon, if epsilon is higher than that difference then the numbers are most likely equal.

1
2
3
4
bool AreSame(float a, float b)
{
    return abs(a - b)  <  std::numeric_limits<float>::epsilon(); //don't forget to include <limits> and <cmath>
}


Comparing float and double numbers is a problematic matter, there are many libraries out there that are focused on comparing floating-point numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    {
        const float n = -0.002f ;
        std::cout << std::fixed << std::setprecision(32)
                  << "nearest representable float 0.002f: " << std::nexttoward( n, n ) << '\n'
                  << "                   it lies between: " << std::nexttoward( n, -100 ) << '\n'
                  << "                               and: " << std::nexttoward( n, +100 )
                  << "\n------------------\n" ;
    }

    {
        const double n = -0.002 ;
        std::cout << std::fixed << std::setprecision(64)
                  << "nearest representable double 0.002: " << std::nexttoward( n, n ) << '\n'
                  << "                   it lies between: " << std::nexttoward( n, -100 ) << '\n'
                  << "                               and: " << std::nexttoward( n, +100 )
                  << "\n------------------\n\n" ;
    }

    std::cout << std::boolalpha
              << " -0.002f == -0.002f ? " << ( -0.002f == -0.002f ) << "\n\n" // true (foat==float)
              << " -0.002f == -0.002  ? " << ( -0.002f == -0.002 ) << "\n\n" // false (float==double)
              << " -0.002  == -0.002  ? " << ( -0.002 == -0.002 ) << '\n' ; // true (double==double)
}


Typical results( IEC 559 / IEEE 754 ):
nearest representable float 0.002f: -0.00200000009499490261077880859375
                   it lies between: -0.00200000032782554626464843750000
                               and: -0.00199999986216425895690917968750
------------------
nearest representable double 0.002: -0.0020000000000000000416333634234433702658861875534057617187500000
                   it lies between: -0.0020000000000000004753142324176451438688673079013824462890625000
                               and: -0.0019999999999999996079524944292415966629050672054290771484375000
------------------

 -0.002f == -0.002f ? true

 -0.002f == -0.002  ? false

 -0.002  == -0.002  ? true

http://coliru.stacked-crooked.com/a/5d571a2d18f7dce2
leryss and JLBorges, Thank you so much for your quick reply. I suspected something same but I ended up thinking the rounding off would have been automatically taken into account wrt number of digits.
Topic archived. No new replies allowed.