IEEE Floating-point quiz!
-
Continuing the discussion from NULL: the worst mistake of computer science:
Quick NaN quiz. Check the Twitter threads for the answers. https://twitter.com/rygorous/status/638517975956393984 - https://twitter.com/rygorous/status/638522085082447872
Pop quiz: given rsqrt(x)=1/sqrt(x), which of these, if any, is true for IEEE floats x?
- rsqrt(x) >= 0
- !(rsqrt(x) < 0)
- sign_bit(rsqrt(x)) == 0
(give rationale and/or counterexamples) (OF COURSE this is a trick question)
Follow-up: which of these tests, if any, are sufficient to guarantee "rsqrt(x) >= 0"?
- if (x >= 0)
- if (!(x < 0))
- if (x > 0)
- if (!(x <= 0))
-
Here's my take on this.
Quiz:
- false: rsqrt(-1) is NaN (the square root of a negative real should be a complex, so it's NaN) and any comparison with NaN returns false
- true: rsqrt(0) = +infinity (since -0 exists, plain 0 is +0), rsqrt(+infinity) = 0 and if x is finite and > 0, so is rsqrt(x); if x <0 or x is NaN, then rsqrt(x) is also NaN, rsqrt(x) < 0 is false and its negation is true
- false: the sign bit of a NaN doesn't matter, so it might be anything
Follow-up:
- yes: see 2 above
- no: NaNs pass this test
- yes: it's stricter than 1
- no: NaNs pass this test
-
The answers are all 42.
-
Nice, although the parenthesis on 2 is questionable (-0 exists, therefore x isn't -0?).
-
The parenthetical is "even though -0 exists, unadorned 0 is presumptivly +0"
-
Yes, that's what I meant in the parenthetical. However, x = -0 still makes me wrong, because according to Wikipedia, sqrt(-0) is -0 (rather than NaN or +0), so rsqrt(-0) is -infinity (and -0 compares as equal to +0).
-
https://what.thedailywtf.com/t/i-vs-i-1/9028/114
Anyway, by correcting the error in my post, but not the one in the post I was referring to, you have failed the pedantry test. Congratulations π!
-
2) true: rsqrt(0) = +infinity (since -0 exists, plain 0 is +0), rsqrt(+infinity) = 0 and if x is finite and > 0, so is rsqrt(x); if x <0 or x is NaN, then rsqrt(x) is also NaN, rsqrt(x) < 0 is false and its negation is true
No, x could be -Infinity
1) yes: see 2 above
Nope, rsqrt(-0) can be -0, and -0 >= 0 is true
-
Soβ¦ the first part of the quiz has all three questions being answered βfalseβ. Negative zero is a supreme counterexample, as
rsqrt(-0.0) = -Inf
and that just messes with everyone's heads.I'd guess (for the second part) that the
x > 0
check is the only usable one. It excludes all the zeroes and NaN variations (comparisons with NaN are always false), and the only special case then is positive Inf and that goes to zero so we're OK. Yes, we could have something nasty at the bottom end of the scale with denormalized numbers, but they'll just get converted to positive zero by the square root, and hence to positive Inf, which is fine.
-