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)
{
}
if float > float, , What precision does "If" have
Re: if float > float, , What precision does "If" have
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."
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: Baidu [Spider] and 272 guests