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?

    1. rsqrt(x) >= 0
    2. !(rsqrt(x) < 0)
    3. 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"?

    1. if (x >= 0)
    2. if (!(x < 0))
    3. if (x > 0)
    4. if (!(x <= 0))


  • Here's my take on this.

    Quiz:

    1. 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
    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
    3. false: the sign bit of a NaN doesn't matter, so it might be anything

    Follow-up:

    1. yes: see 2 above
    2. no: NaNs pass this test
    3. yes: it's stricter than 1
    4. 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 🎉!



  • @Khudzlin said:

    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

    @Khudzlin said:

    1) yes: see 2 above

    Nope, rsqrt(-0) can be -0, and -0 >= 0 is true


  • Discourse touched me in a no-no place

    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. :smile:

    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.



  • :+1:


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.