signum() not in math?

Joal Heagney s713221 at student.gu.edu.au
Sat Oct 13 22:25:45 EDT 2001


Marcin 'Qrczak' Kowalczyk wrote:
> 
> Fri, 12 Oct 2001 11:27:16 +0200, Dinu Gherman <gherman at darwin.in-berlin.de> pisze:
> 
> > How comes there is no sign() or signum() function in math?
> 
> You could use cmp(x,0) if it was guaranteed that it returns only
> -1,0,1. Unfortunately it's not said in docs; it happens to work
> though. Perhaps docs should be changed...
> 
> --
>  __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
>  \__/
>   ^^                      SYGNATURA ZASTÊPCZA
> QRCZAK

Sweet. That gets dropped into my python scrapbook!
And for the picky:
>>> import math
>>> math.sign = lambda x: cmp(x,0)

However, it chucks a hissyfit if you try to use complexes, as it should
- now if only I could get a better error message

>>> math.sign(1+1j)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <lambda>
TypeError: cannot compare complex numbers using <, <=, >, >=
>>>
-- 
      Joal Heagney is: _____           _____
   /\ _     __   __ _    |     | _  ___  |
  /__\|\  ||   ||__ |\  || |___|/_\|___] |
 /    \ \_||__ ||___| \_|! |   |   \   \ !



More information about the Python-list mailing list