Page 1 of 1

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

Posted: Wed Jan 11, 2023 2:51 am
by username
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)
{
}

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

Posted: Wed Jan 18, 2023 7:19 pm
by Hackswell
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."