in reply to Re^2: Spaceship Zero
in thread Spaceship Zero
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.$delta_to_add = $magnitude * $direction; $value += $delta_to_add;
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Spaceship Zero (>=0)
by pKai (Priest) on Nov 10, 2008 at 08:51 UTC | |
|
Re^4: Spaceship Zero
by Anonymous Monk on Nov 28, 2008 at 10:48 UTC |