New python module to simulate arbitrary fixed and infinite precisionbinary floating point

Discussion in 'Python' started by Rob Clewley, Aug 10, 2008.

  1. Rob Clewley

    Rob Clewley Guest

    Dear Pythonistas,

    How many times have we seen posts recently along the lines of "why is
    it that 0.1 appears as 0.10000000000000001 in python?" that lead to
    posters being sent to the definition of the IEEE 754 standard and the module? I am teaching an introductory numerical analysis
    class this fall, and I realized that the best way to teach this stuff
    is to be able to play with the representations directly, in particular
    to be able to see it in action on a simpler system than full 64-bit
    precision, especially when str(f) or repr(f) won't show *all* of the
    significant digits stored in a float. The decimal class deliberately
    avoids binary representation issues, and I can't find what I want

    Consequently, I have written a module to simulate the machine
    representation of binary floating point numbers and their arithmetic.
    Values can be of arbitrary fixed precision or infinite precision,
    along the same lines as python's in-built decimal class. The code is

    The design is loosely based on that decimal module, although it
    doesn't get in to threads, for instance. You can play with different
    IEEE 754 representations with different precisions and rounding modes,
    and compare with infinite precision Binary numbers. For instance, it
    is easy to learn about machine epsilon, representation/rounding error
    using a much simpler format such as a 4-bit exponent and 6-bit
    mantissa. Such a format is easily defined in the new module and can be
    manipulated easily:
    Binary("0", (4, 6, ROUND_DOWN))
    0 0000 000000
    Binary("0.001E-9", (4, 6, ROUND_DOWN))
    0 0000 000001
    Binary("0.111111E-6", (4, 6, ROUND_DOWN))
    0 0011 100110 rounded to 0.099609375
    Binary("0.1E-6", (4, 6, ROUND_DOWN))

    The usual arithmetic operations are permitted on these objects, as
    well as representations of their values in decimal or binary form.
    Default contexts for half, single, double, and quadruple IEEE 754
    precision floats are provided. Binary integer classes are also
    provided, and some other utility functions for converting between
    decimal and binary string representations. The module is compatible
    with the numpy float classes and requires numpy to be installed.

    The source code is released under the BSD license, but I am amenable
    to other licensing ideas if there is interest in adapting the code for
    some other purpose. Full details of the functionality and known issues
    are in the module's docstring, and many examples of usage are in the
    accompanying file (which also acts to validate the
    common representations against the built-in floating point types). I
    look forward to hearing feedback, especially in case of bugs or
    suggestions for improvements.


    Robert H. Clewley, Ph. D.
    Assistant Professor
    Department of Mathematics and Statistics
    Georgia State University
    720 COE, 30 Pryor St
    Atlanta, GA 30303, USA

    tel: 404-413-6420 fax: 404-413-6403
    Rob Clewley, Aug 10, 2008
    1. Advertisements

  2. I would be interested to look at that, if I can find the time.

    Is this related to minifloats?
    Steven D'Aprano, Aug 11, 2008
    1. Advertisements

  3. Rob Clewley

    Rob Clewley Guest

    Strictly speaking, yes, although after a brief introduction to the
    general idea, the entry on that page focuses entirely on the
    interpretation of the values as integers. My code *only* represents
    the values in the same way as the regular-sized IEEE 754 formats, i.e.
    the smallest representable number is a fraction < 1, not the integer
    1. I haven't supplied a way to use my classes to encode integers in
    this way, but it wouldn't be hard for someone to add that
    functionality in a sub-class of my ContextClass.

    Thanks for pointing out that page, anyway. I didn't know the smaller
    formats had been given their own name.

    Rob Clewley, Aug 11, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.