I want to test if a number is positive or negative, especially also in the case of zero. IEEE-754 allows for -0.0
, and it is implemented in Python.
The only workarounds I could find were:
def test_sign(x):
return math.copysign(1, x) > 0
And maybe (probably takes longer to run):
def test_sign(x):
math.atan2(x, -1)
I could not find a dedicated function anywhere in the usual libraries, did I overlook something?
Edit: (Why this was relevant)
This is not my current plan anymore, but when asking the question I tried to overload a function depending on whether an argument was positive or negative. Allowing the user to pass negative zeros would resolve the ambiguity what was meant for zero-valued input. And I think this may be of general interest for other use cases as well.