This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients docs@python, mark.dickinson, r.david.murray, terry.reedy, umedoblock
Date 2011-06-04.23:35:36
SpamBayes Score 1.0868972e-11
Marked as misclassified No
Message-id <1307230537.74.0.377698331949.issue12211@psf.upfronthosting.co.za>
In-reply-to
Content
> "Return a float with the magnitude of x but the sign of y."
This appears to describe both current behavior and what I believe was the intention. I would go with a doc patch based on this.
umedoblock, go ahead and make one.

It occurred to me, also, that as currently written, copysign 'should' return the type of the first arg. In C89, and I suspect in Python 1.0, all math functions return double (Python float). Like Mark, I am more inclined to change the doc than the code.

1. One use of copysign is to give a correctly signed 0.0. This does not apply to ints, where 0 is always unsigned. I do not know of any use of copysign in number (count/int) theory. (There could be, of course.)

2. icopysign(-1,0) == +1 (sign added for emphasis), whereas I believe that for ints, the answer should be -1, as 0 has no sign.

def icopysign(j,k):
    if (j > 0) and (k < 0) or (j < 0) and (k > 0):
        return -j
    return j
for j,k,i in ((1,1,1), (1,-1,-1), (-1,-1,-1), (-1, 1, 1),
              (1,0,1), (-1,0,-1)):
    assert icopysign(j,k) == i, (j,k,i)

This would certainly be up for debate if we changed the code, but there should be no difference in outputs for same value inputs. (This principle is the reason for / and //.) So lets leave copysign as a function defined on floats with inputs coerced to float if needed. Anyone who needs it for ints can define it for (-1,0) according to their need.
History
Date User Action Args
2011-06-04 23:35:37terry.reedysetrecipients: + terry.reedy, mark.dickinson, r.david.murray, docs@python, umedoblock
2011-06-04 23:35:37terry.reedysetmessageid: <1307230537.74.0.377698331949.issue12211@psf.upfronthosting.co.za>
2011-06-04 23:35:37terry.reedylinkissue12211 messages
2011-06-04 23:35:36terry.reedycreate