CS 3723
  Programming Languages  
  13. Multi-Precision Float   


13.1. Multi-Precision Float: Python lets ints be arbitrarily large (subject to available memory). Ordinary Python can't handle floating point numbers with more significant digits than a double (about 16), so some other software is required:

  • mpmath: a third-party addition to Python, this seems the best choice. I installed it on my Linux system (using Python 2.6.5) at home with no trouble. It is available for Python 2.5 or higher, including Python 3.x. My install required root privileges, so I cannot install it on an elk, but I'll see if the systems people will do that. Besides Linux, it should also install easily on an apple, but I haven't tried that. Also available for windows. It is pure-Python code, and seems very sophisticated, with a lot of fancy capabilities and features. I'm just getting started on this (see below). See also mpmath documentation. Here are a few computations:

    mpmath: Sample Calculations
    # mpmath_test.py
    from mpmath import *
    import sys
    
    mp.dps = 50 # 50 digit calculations
    print "Precision: ", mp.dps
    print mpf(2) ** mpf('0.5') # sqrt(2)
    print 2*pi # 2*pi
    near_int = e ** (pi * mpf(163) ** mpf('0.5'))
    print near_int # very close to an int
    
    % Python mpmath.py
    Precision:  50
    1.4142135623730950488016887242096980785696718753769
    6.2831853071795864769252867665590057683943387987502
    262537412640768743.99999999999925007259719818568887
    
    (all digits above are correct)
    

  • Others: Python has other such libraries, as do C and Lisp and many others.

(Revision date: 2014-07-04. Please use ISO 8601, the International Standard.)