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.

classification
Title: add math.sign/signum
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: math.signum(int)
View: 829370
Assigned To: Nosy List: AnkurDedania, r.david.murray, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2016-08-11 16:10 by AnkurDedania, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg272459 - (view) Author: Ankur Dedania (AnkurDedania) Date: 2016-08-11 16:10
Add a new function to the math module sign/signum https://en.wikipedia.org/wiki/Sign_function
msg272460 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-11 16:33
See math.copysign().

https://docs.python.org/3/library/math.html#math.copysign
msg272461 - (view) Author: Ankur Dedania (AnkurDedania) Date: 2016-08-11 16:59
sign/signum isn't the same as copysign, and doesn't support complex numbers
msg272462 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-11 17:10
sgn(x) is the same as copysign(1.0, x).

The math module doesn't support complex numbers and shouldn't.
msg272463 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-08-11 17:19
If this is added, should this signum function be the two value version that returns 1 for zero, or the three value version that returns 0? Should it distinguish between signed zeroes +0.0 and -0.0? What should it do for NANs (raise, return a NAN, copy the sign bit from the NAN)?

The argument for adding this will be slightly stronger if other languages have this, and especially if it is part of the standard C maths library.

In any case, there's only a few more days until the 3.6 feature freeze, so unless you have an implementation and tests ready to go, the earliest it could be added will be 3.7.

Serhiy: if this gets added, then it would make sense to add cmath.signum to handle complex numbers.
msg272466 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-11 18:02
copysign() is part of the standard C maths library (that is why it was added to the math module).

sgn/sign/signum was used in old languages that didn't have copysign() (Basic, Pascal, etc).
msg272477 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-11 19:55
This has been previously discussed and rejected, in issue 829370.
msg272654 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-08-14 07:32
Thanks David.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71926
2016-08-14 07:32:37serhiy.storchakasetmessages: + msg272654
2016-08-11 19:55:10r.david.murraysetstatus: open -> closed

superseder: math.signum(int)

nosy: + r.david.murray
messages: + msg272477
resolution: duplicate
stage: resolved
2016-08-11 18:02:32serhiy.storchakasetmessages: + msg272466
2016-08-11 17:19:54steven.dapranosetnosy: + steven.daprano
messages: + msg272463
2016-08-11 17:10:24serhiy.storchakasetmessages: + msg272462
2016-08-11 16:59:18AnkurDedaniasetmessages: + msg272461
2016-08-11 16:33:53serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg272460
2016-08-11 16:10:33AnkurDedaniacreate