473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.1000000000000 0001 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_denor malized = context('0 0000 111111') # direct spec of the sign, characteristic, and significand bits
largest_denor malized
Binary("0.11111 1E-6", (4, 6, ROUND_DOWN))
>>largest_denor malized.as_deci mal()
Decimal("0.0153 80859375")
>>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_dec imal() # 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 1897
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.1000000000000 0001 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
5154
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 0.90000000000000013
10
3694
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. Andrew dalke@dalkescientific.com
14
3075
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 trailing 0's..how would i do that? _________________________________________________________________
0
243
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 ( +2) / 233 closed ( +2) / 457 total ( +4) New / Reopened Patches ______________________
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.