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

R

Rob Clewley

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
decimal.py 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
online.

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
here: http://www2.gsu.edu/~matrhc/binary.html

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))
Decimal("0.015380859375")
0 0011 100110 rounded to 0.099609375
Decimal("-23.0078125")
Decimal("-23.0000")
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 binary_tests.py (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.

-Rob

--
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
http://www2.gsu.edu/~matrhc
http://brainsbehavior.gsu.edu/
 
S

Steven D'Aprano

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 decimal.py
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 ....
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 here:
http://www2.gsu.edu/~matrhc/binary.html


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

Is this related to minifloats?

http://en.wikipedia.org/wiki/Minifloat
 
R

Rob Clewley


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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top