Floating point C: can x / x be> 1.0?
In C, can you exceed 1.0 if you have a number divided by yourself? Basically, can x / x (like 5.1 / 5.1) exceed 1.0? x can be float, double, or long double. Note that x / x will not be a literal code like the variable x.
I tried to find this answer but it is difficult to find suitable search terms.
source to share
Assuming IEEE-754 compliance, x/x
it is always exact 1.0
* because division is a valid rounding operation, which means it returns the floating point number that is closest to an "infinitely precise" mathematical value.
However, there are a few bugs. For example, if FLT_EVAL_METHOD
it is not equal to zero in your C implementation, but is x
in fact a type expression (a+b)/(a+b)
, then it can be assumed (unlikely, but it has been noted) that, with certain optimization settings, the result may not be exactly equal 1.0
. And of course, if your compiler is not IEEE-754 compliant, all bets are off.
[*] or NaN if x is zero or infinity or NaN.
source to share