Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT:</strong></p> <p>Indeed there was a <a href="http://bugs.python.org/msg58786" rel="noreferrer">patch</a> which included <code>sign()</code> in <a href="http://docs.python.org/library/math.html#math.copysign" rel="noreferrer">math</a>, but it wasn't accepted, because they didn't agree on <a href="http://bugs.python.org/msg59137" rel="noreferrer">what it should return in all the edge cases</a> (+/-0, +/-nan, etc)</p> <p>So they decided to implement only copysign, which (although more verbose) can be <a href="http://bugs.python.org/msg59154" rel="noreferrer">used to delegate to the end user the desired behavior for edge cases</a> - which <a href="http://bugs.python.org/msg59152" rel="noreferrer">sometimes might require the call to <code>cmp(x,0)</code></a>.</p> <hr> <p>I don't know why it's not a built-in, but I have some thoughts.</p> <pre><code>copysign(x,y): Return x with the sign of y. </code></pre> <p>Most importantly, <code>copysign</code> is a superset of <code>sign</code>! Calling <code>copysign</code> with x=1 is the same as a <code>sign</code> function. So you could just use <code>copysign</code> and <em>forget about it</em>.</p> <pre><code>&gt;&gt;&gt; math.copysign(1, -4) -1.0 &gt;&gt;&gt; math.copysign(1, 3) 1.0 </code></pre> <p>If you get sick of passing two whole arguments, you can implement <code>sign</code> this way, and it will still be compatible with the IEEE stuff mentioned by others:</p> <pre><code>&gt;&gt;&gt; sign = functools.partial(math.copysign, 1) # either of these &gt;&gt;&gt; sign = lambda x: math.copysign(1, x) # two will work &gt;&gt;&gt; sign(-4) -1.0 &gt;&gt;&gt; sign(3) 1.0 &gt;&gt;&gt; sign(0) 1.0 &gt;&gt;&gt; sign(-0.0) -1.0 &gt;&gt;&gt; sign(float('nan')) -1.0 </code></pre> <p>Secondly, usually when you want the sign of something, you just end up multiplying it with another value. And of course that's basically what <code>copysign</code> does.</p> <p>So, instead of:</p> <pre><code>s = sign(a) b = b * s </code></pre> <p>You can just do:</p> <pre><code>b = copysign(b, a) </code></pre> <p>And yes, I'm surprised you've been using Python for 7 years and think <code>cmp</code> could be so easily removed and replaced by <code>sign</code>! Have you never implemented a class with a <code>__cmp__</code> method? Have you never called <code>cmp</code> and specified a custom comparator function?</p> <p>In summary, I've found myself wanting a <code>sign</code> function too, but <code>copysign</code> with the first argument being 1 will work just fine. I disagree that <code>sign</code> would be more useful than <code>copysign</code>, as I've shown that it's merely a subset of the same functionality.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload