Feature or enhancement
Proposal:
I am proposing to add a math.sign() function to the standard math module. This function returns the sign of a number: -1 for negative values, 1 for positive values, and 0 for zero.
While this can be implemented in pure Python, a native C implementation provides better performance and, more importantly, a standardized way to handle edge cases that are often implemented inconsistently by users.
Key features of the proposed implementation:
Full IEEE 754 Support: Correctly handles NaN, signed zeros (+/-0.0), and infinities. For NaN input, it returns NaN.
Duck-typing via Rich Comparisons: The implementation is not limited to int and float. It works with any object that supports order comparisons (via > 0, < 0, and ==0 logic), including (but not restricted by) Decimal, Fraction, sympy.Rational, numpy types, and custom user classes.
Robust Error Handling: Provides informative TypeError messages when an argument doesn't support comparison, including the "Inner error" if the object's comparison protocol raises an exception.
Performance: Implemented in C as part of mathmodule.c using Argument Clinic.
Implementation status: I have a complete implementation ready and tested, including:
- C code in
mathmodule.c.
- Comprehensive test suite in
test_math.py covering all edge cases.
- Documentation updates for
math.rst.
- Verified with
regrtest -R 3:3 on a debug build (no leaks detected).
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/include-math-sign/45404/92
The discussion was slow because the topic is set to "slow mode", and, I think, due to Christmas. I performed the full implementation quicker than we could discuss details.
Linked PRs
Feature or enhancement
Proposal:
I am proposing to add a
math.sign()function to the standard math module. This function returns the sign of a number:-1for negative values,1for positive values, and0for zero.While this can be implemented in pure Python, a native C implementation provides better performance and, more importantly, a standardized way to handle edge cases that are often implemented inconsistently by users.
Key features of the proposed implementation:
Full IEEE 754 Support: Correctly handles
NaN, signed zeros (+/-0.0), and infinities. ForNaNinput, it returnsNaN.Duck-typing via Rich Comparisons: The implementation is not limited to
intandfloat. It works with any object that supports order comparisons (via> 0,< 0, and==0logic), including (but not restricted by)Decimal,Fraction,sympy.Rational,numpytypes, and custom user classes.Robust Error Handling: Provides informative
TypeErrormessages when an argument doesn't support comparison, including the "Inner error" if the object's comparison protocol raises an exception.Performance: Implemented in C as part of
mathmodule.cusing Argument Clinic.Implementation status: I have a complete implementation ready and tested, including:
mathmodule.c.test_math.pycovering all edge cases.math.rst.regrtest -R 3:3on a debug build (no leaks detected).Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/include-math-sign/45404/92
The discussion was slow because the topic is set to "slow mode", and, I think, due to Christmas. I performed the full implementation quicker than we could discuss details.
Linked PRs