Child pages
  • Hyak_python_decimal
Skip to end of metadata
Go to start of metadata

If you need to handle numbers which are very large or very small and outside the range of the python double precision type then you can use the Python cdecimal module. The documentation is same as the Python standard library decimal module but the cdecimal module is implemented in C so it is faster. In Python 2.7 cdecimal is written in C and decimal is written in Python. In Python 3.3 and later, decimal is written in C so just use decimal instead of cdecimal.  (Alternative modules are mpmath, gmpy2, bigfloat.)

  import cdecimal as cd

  x=cd.Decimal('2.5')

  y=x**10000

  y

Decimal('2.512388057698744585180135042E+3979')

 

Note about numpy.longdouble and numpy.float128:

numpy.longdouble and numpy.float128 have same range as double precision.

They cannot handle very large or very small numbers.

>>> import numpy      

>>> numpy.longdouble

<type 'numpy.float128'>

>>> x=numpy.longdouble(10e300)

>>> y=numpy.longdouble(10e300)

>>> x+y

2.000000000000000105e+301

>>> y=numpy.longdouble(10e307)

>>> y

1.000000000000000011e+308

>>> y=numpy.longdouble(10e308)

>>> y

inf

>>> y=numpy.float128(10e308)

>>> y

inf

>>>                        

 

 

  • No labels