Can bad stuff happen when dividing 1/a very small float?
Posted
by Jeremybub
on Stack Overflow
See other posts from Stack Overflow
or by Jeremybub
Published on 2010-06-02T02:05:54Z
Indexed on
2010/06/02
2:13 UTC
Read the original article
Hit count: 296
If I want to check that positive float A is less than the inverse square of another positive float B (in C99), could something go wrong if B is very small?
I could imagine checking it like
if(A<1/(B*B))
but if B is small enough, would this possibly result in infinity? If that were to happen, would the code still work correctly in all situations?
in a similar vein, I might do
if(1/A>B*B)
Which might be slightly better because B*B might be zero if B is small (is this true?)
Finally, a solution that I can't imagine being wrong is
if(sqrt(1/A)>B)
Which I don't think would ever result in zero division, but still might be problematic if A is close to zero.
So basically, my questions are
Can 1/X ever be infinity if X is greater than zero (but small)?
Can X*X ever be zero if X is greater than zero?
Will comparisons with infinity work the way I would expect them to?
© Stack Overflow or respective owner