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)


In reply to Re^3: Spaceship Zero by graff
in thread Spaceship Zero by Orchew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.