Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Spaceship Zero

by ikegami (Patriarch)
on Nov 10, 2008 at 09:35 UTC ( [id://722593]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Spaceship Zero
in thread Spaceship Zero

Care to explain what you mean that it won't handle zeros?

I think he's referring to the discrepancy between the documented behaviour (returning the sign of the number) and the actual behaviour (returning 0) for both positive and negative zeros.

$ perl -le'$x=+0.0; print $x; print $x<=>0' 0 0 $ perl -le'$x=-0.0; print $x; print $x<=>0' -0 0

It does produce the documented results for +inf and -inf, and a sane result for NaN.

$ perl -wle'$x=0+Infinity; print $x; print $x<=>0' inf 1 $ perl -wle'$x=0-Infinity; print $x; print $x<=>0' -inf -1 $ perl -wle'$x=0+NaN; print $x; print $x<=>0' nan Use of uninitialized value in print at -e line 1.

Replies are listed 'Best First'.
Re^4: Spaceship Zero
by pKai (Priest) on Nov 10, 2008 at 11:44 UTC

    The sign function (for real numbers in mathematics) is defined in relation to numerical comparison to zero.

    Any other meaning of sign refering to visual representations has little practical value, IMHO: should sgn("-(-1)") be -1 ?

    So "returning the sign" as the "documented behaviour" for me matches exactly the behaviour of the spaceship zero

      The OP didn't say sign function, he said sign of a number. In floating point numbers, every number including zero has a sign. -0 is a different number than 0. It's not a visual representation. It's used in underflows. n = 0 means 0 ≤ n < {smallest positive}. n = -0 means {smallest negative} < n ≤ 0

        The OP didn't say sign function, he said sign of a number.

        Actually, he said sgn($x) three times.

        In real numbers, 0 is neither positive nor negative. The fiction of "negative zero" in some floating point implementations can be interesting but doesn't trump the existence of the Real number system.

        And that the OP wanted 0 returned for the "sign" of zero is quite clearly stated in multiple ways, despite your misinterpretting "sign of a number" to mean "sign of a floating point number (in an implementation that implements negative zero and 'treats negative zero consistently in arithmetic operations')". The ability to get -1 from "negative zero" on some systems is surely useful in some cases. But it is certainly not the only useful result.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://722593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found