if float > float, , What precision does "If" have

username
Posts: 507
Joined: Thu May 03, 2018 1:18 pm

if float > float, , What precision does "If" have

Postby username » Wed Jan 11, 2023 2:51 am

When checking float values with an if then, how many places to the right of the decimal does if then check ?
float x= 1.0020002;
float y= 1.002;

if(x > y)
{
}

Hackswell
Posts: 14
Joined: Wed Jun 03, 2020 8:13 pm

Re: if float > float, , What precision does "If" have

Postby Hackswell » Wed Jan 18, 2023 7:19 pm

IEEE floats don't have "decimal" precision. It depends on the size of the mantissa, and other technical things. What you should do is determine the range of typical values and the allowable tolerance, and do something more along the line of:

if ( abs(x-y) < max_error_tolerable ) { ... } // equiv to if x == y

There's a big difference between the errors between these sets of numbers in float land:

1.000272394872 ( probably 5-7 decimal places accuracy )
129348947208123428923049823409283490342.0273428927 ( quite possible only a few decimal places due to need to keep whole number significant digits)

This post has a couple of good answers:
https://stackoverflow.com/questions/491 ... comparison

"Comparing for greater/smaller is not really a problem unless you're working right at the edge of the float/double precision limit."

Who is online

Users browsing this forum: No registered users and 348 guests