473,418 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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:
>>context = define_context(4, 6, ROUND_DOWN)
>>zero = context(0)
Binary("0", (4, 6, ROUND_DOWN))
>>print zero # sign, characteristic, significand bits
0 0000 000000
>> zero.next()
Binary("0.001E-9", (4, 6, ROUND_DOWN))
>>print zero.next()
0 0000 000001
>>largest_denormalized = context('0 0000 111111') # direct spec of the sign, characteristic, and significand bits
largest_denormalized
Binary("0.111111E-6", (4, 6, ROUND_DOWN))
>>largest_denormalized.as_decimal()
Decimal("0.015380859375")
>>n01 = context(0.1) # nearest representable is actually stored
>>print n01, " rounded to ", n01.as_decimal()
0 0011 100110 rounded to 0.099609375
>>Binary('-10111.0000001').as_decimal()
Decimal("-23.0078125")
>>Binary('-10111.0000001', context).as_decimal() # not enough precision in this context
Decimal("-23.0000")
>>diff = abs(Binary('-10111.0000001') - Binary('-10111.0000001', context))
diff
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/
Aug 10 '08 #1
2 1873
On Sun, 10 Aug 2008 16:34:34 -0400, Rob Clewley wrote:
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
--
Steven

Aug 11 '08 #2
>
Is this related to minifloats?

http://en.wikipedia.org/wiki/Minifloat
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
Aug 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
14
by: chun ping wang | last post by:
Hey i have a stupid question. How do i get python to print the result in only three decimal place... Example>>> round (2.995423333545555, 3) 2.9950000000000001 but i want to get rid of all...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 401 open ( +3) / 3342 closed ( +8) / 3743 total (+11) Bugs : 896 open ( -8) / 6035 closed (+24) / 6931 total (+16) RFE : 224 open...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.