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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
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
| [reply] |
I think there may be some justifiable confusion between something like the "sgn()" function in BASIC, which does work just like the spaceship operator (returning -1 or 0 or 1), and something else that is more like the "copysign" function in C. There's probably a natural tendency to want to do something like this:
$delta_to_add = $magnitude * $direction;
$value += $delta_to_add;
where direction is "1" for "up" and "-1" for "down"; but imagine a case where $magnitude is not zero, and $direction could be derived from or based on any value whatsoever, such that anything non-negative should count as "positive". You want to "copy" the sign bit from $direction onto $magnitude, but Perl doesn't really have a "copysign" function, and your "standard sgn()" function (like the spaceship operator) would do the wrong thing by returning zero when basis for $direction is zero, thereby setting $delta_to_add to zero as well.
I used to think there was a function that would return -1 for values less than 0, and 1 otherwise, so that you could simply use the multiplication operator to copy the sign of one variable onto another. I also dimly recall at least one discussion about this at the Monastery, but now I can't seem to locate either such a function or the previous debate. Oh well...
(updated slightly in hopes of making the 2nd paragraph clearer) | [reply] [d/l] |
It seems obvious that
>= is a different operator than <=>.
If your want to test for something being >=0 and call it the copysign function, it's O. K., but the revelation of the OP is that spaceship zero implements the well-known Sign function abbreviated as sgn() in mathematics (not just some builtin "in BASIC".)
| [reply] [d/l] |
Wouldn't that be:
$x<=>0||1
(Also; quite short)
| [reply] [d/l] |