Can bad stuff happen when dividing 1/a very small float?
- by Jeremybub
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?