On Numbers

Steve Holden steve at holdenweb.com
Wed Jan 18 07:25:31 EST 2006


Erik Max Francis wrote:
> Steven D'Aprano wrote:
> 
> 
>>Perhaps you meant to write (-1)**0.5, in which case Python developers have
>>a decision to make: should it assume real-valued maths unless explicitly
>>told differently, and hence raise an exception, or coerce the result to
>>complex?
>>
>>In this case, Python raises an exception, as it should, unless you
>>explicitly uses complex numbers. That's the best behaviour for the
>>majority of people: most people don't even know what complex numbers are,
>>let alone want to deal with them in their code. Python, after all, is not
>>Mathematica.
> 
> 
> Note that cmath.sqrt returns the expected complex result for 
> cmath.sqrt(-1.0).
> 
  >>> import cmath
  >>> cmath.sqrt(-1)
1j
  >>>

Indeed. But even exponentiation can come respectably close:

  >>> (-1+0j)**0.5
(6.123233995736766e-17+1j)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list