[Python-3000] Math in Python 3.0

Nick Coghlan ncoghlan at gmail.com
Sun May 14 03:01:20 CEST 2006


Fredrik Johansson wrote:
> For example, square roots are known as math.sqrt(x) for floats,
> cmath.sqrt(x) for complex numbers, x.sqrt() for decimals, and
> gmpy.sqrt(x)/gmpy.fsqrt(x) for gmpy's types. Oh, and SciPy has its own
> sqrt function that works on arrays (but not Decimals or gmpy's types).

Py3k's function overloading should fix this:

@overloaded
def sqrt(value):
     raise TypeError("Cannot take square root of %s" % type(value).__name__)

@sqrt.overload
def sqrt_float(value : float):
     return math.sqrt(value)

@sqrt.overload
def sqrt_complex(value : complex):
     return cmath.sqrt(value)

@sqrt.overload
def sqrt_decimal(value : decimal):
     return value.sqrt()

# Similar overloads can be added for the types in gmpy and numpy.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list