473,386 Members | 1,969 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,386 software developers and data experts.

Precise calculations

Could anybody help me on how to do precise calculations with Python the way
hand-held calculators do?

A small example to show the impreciseness coming from the floating point
arithmetics:

(100000000000000.4*3)*100 gives 30000000000000124.0

Thanks for any help,

Károly
Jul 18 '05 #1
4 1991
On Fri, 07 Nov 2003 13:17:44 GMT, Ladvánszky Károly wrote:
A small example to show the impreciseness coming from the floating
point arithmetics:
Issues with representing floating point numbers in binary computers are
discussed here with respect to Python:

<http://www.python.org/doc/current/tut/node14.html>
Could anybody help me on how to do precise calculations with Python
the way hand-held calculators do?


Use rounding, e.g. as implemented by the str() function. From the above
URL:

"Python's builtin str() function produces only 12 significant
digits, and you may wish to use that instead. It's unusual for
eval(str(x)) to reproduce x, but the output may be more pleasant to
look at:
print str(0.1)

0.1

It's important to realize that this is, in a real sense, an
illusion: the value in the machine is not exactly 1/10, you're
simply rounding the display of the true machine value."

--
\ "I know when I'm going to die, because my birth certificate has |
`\ an expiration date." -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
On 8 Nov 2003 08:26:07 +1050, Ben Finney wrote:
On Fri, 07 Nov 2003 13:17:44 GMT, Ladvánszky Károly wrote:
Could anybody help me on how to do precise calculations with Python
the way hand-held calculators do?


Use rounding, e.g. as implemented by the str() function.


Apologies; str() doesn't do rounding per se, it shows the float values
with fewer significant digits without changing the internal value.

--
\ "Once consumers can no longer get free music, they will have to |
`\ buy the music in the formats we choose to put out." -- Steve |
_o__) Heckler, VP of Sony Music, 2001 |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
Ben Finney wrote:
...
Could anybody help me on how to do precise calculations with Python
the way hand-held calculators do?


Use rounding, e.g. as implemented by the str() function. From the above


Use str if you really want to do like hand-held calculators, i.e., hide some
digits. If you want really high precision in binary floating point, the
gmpy extension module may be useful; if in decimal floating point, there is
a class Decimal that's being worked on to eventually provide that, too.
Alex

Jul 18 '05 #4
On Fri, 07 Nov 2003 13:17:44 GMT, "Ladvánszky Károly" <aa@bb.cc> wrote:
Could anybody help me on how to do precise calculations with Python the way
hand-held calculators do?

A small example to show the impreciseness coming from the floating point
arithmetics:

(100000000000000.4*3)*100 gives 30000000000000124.0


By coincidence I just posted a module (very alpha code, see "prePEP: Decimal data type" thread)
that does exact decimal floating point, so you can do better than hand calculators ;-)

First, let's use it to show what the full floating point bits ('all') show when converted
to an exact representation:
from exactdec import ED
ED(100000000000000.4,'all') ED('100000000000000.40625')
^
+--the _actual_ floating point value represented
ED(100000000000000.4,'all')*3 ED('300000000000001.21875')
^
+-- an exact multiple by 3
ED(100000000000000.4*3,'all') ED('300000000000001.25')
^
+--the exact value of the inexact floating point product
ED(100000000000000.4*3,'all')*100 ED('30000000000000125')
^
+--times 100 exactly
ED(100000000000000.4*3*100,'all') ED('30000000000000124')
^
+--the result of the additional floating point multiply

Now doing the math starting with an exact string literal for your starting value:
ED('100000000000000.4') ED('100000000000000.4')
^
+--value is exact
ED('100000000000000.4')*3 ED('300000000000001.2')
^
+--product is exact (the right operand of 3 is converted like ED(3) which is exact
since integer is exact.
(ED('100000000000000.4')*3)*100 ED('30000000000000120')
^
+--similarly with the 100 factor, which is converted to exact before multiplying as well.

Now try this on your calculator (note the binomial thing):
b = ED('1001001001001001001001001001')
b ED('1.001001001001001001001001001e27') b*b ED('1.00200300400500600700800901000900800700600500 4003002001e54') b*b*b

ED('1.00300601001502102803604505506306907307507507 3069063055045036028021015010006003001e81')

;-)

Regards,
Bengt Richter
Jul 18 '05 #5

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

Similar topics

4
by: Targa | last post by:
Trying to total some price fields in a form but doesnt work when all the referenced form fields dont exisit. This is for an invoice - pulled prom a database and the form doesnt always contain the...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
3
by: John M | last post by:
Hi, I've been coming up against a failure of a report to display the result of a simple calculation. I have realised that this calculation cannot take place unless the field I am working on is...
0
by: SJM | last post by:
There is a database which has combo boxes to allow users to select values from pre-set lists of common specifications. The form allows users to enter values that are not in the dropdown lists....
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
13
by: fredrik.ryde | last post by:
Hi, I have a database which contains alot of finacial data. I want to retreive some data, run som calculations with it, nothing complex just simple arithmetic. I wonder if it's faster to let...
1
by: Grubsy4u | last post by:
Grubsy4u Newbie 7 Posts October 5th, 2007 11:31 AM #1 Report calculations --------------------------------------------------------------------------------
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.