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

Precision?

Hello,

I've just wanted to check Python's abilities as a calculator and this
is what came out:
1.0 + 3.0 + 4.6

8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)

best regards
Steffen

Jul 19 '05 #1
9 2509
Steffen Glückselig wrote:
1.0 + 3.0 + 4.6


8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)


You may find annex B of the python tutorial an interesting read:
http://docs.python.org/tut/node16.html
Jul 19 '05 #2
tiissa wrote:
Steffen Glückselig wrote:
> 1.0 + 3.0 + 4.6

8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)


You may find annex B of the python tutorial an interesting read:
http://docs.python.org/tut/node16.html


Yes, the simplest way to get what you are expecting is probably:

py> print 1.0 + 3.0 + 4.6
8.6

The print statement calls str() instead of repr(). In many (most?)
cases, this will print out what you expect it to. But you should be
aware of floating-point representation issues, and you should definitely
read the reference above. If you really do need precise decimal
representation, you can use the 2.4 decimal.Decimal objects:

py> d.Decimal("1.0") + d.Decimal("3.0") + d.Decimal("4.6")
Decimal("8.6")

But if you just want a handy calculator, I'd go with print.

STeVe
Jul 19 '05 #3
"Steffen Glückselig" <us****@gungfu.de> writes:
Hello,

I've just wanted to check Python's abilities as a calculator and this
is what came out:
1.0 + 3.0 + 4.6

8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)


This is as correct as your computer's FPU can made it :)

--
http://www.peter.dembinski.prv.pl
Jul 19 '05 #4
tiissa wrote:
Steffen Glückselig wrote:
>1.0 + 3.0 + 4.6


8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)

You may find annex B of the python tutorial an interesting read:
http://docs.python.org/tut/node16.html


In addition to what you find in the above link, the round function can
be used.

p = 1 #digits of precision after decimal
a, b, c = 1.0, 3.05, 4.6
print round(a+b+c,p)

-> 8.6

You also have the option to use the print statements '%' operator to
format the output.
a, b, c = 1.0, 3.05, 4.6
print "%.1f"%(a+b+c)

-> 8.6
_Ron
Jul 19 '05 #5
If you want to do decimal arithmetic, use the decimal module which is
new in Python 2.4.

Python 2.4 (#1, Jan 22 2005, 20:45:18)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from decimal import Decimal as D
D("1.0") + D("3.0") + D("4.6") Decimal("8.6")
when you write '4.6', you get a binary floating-point number which is
not equal to the decimal number 4.6. 4.6 4.5999999999999996 4.6 == D("4.6")

False

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCh720Jd01MZaTXX0RAoZZAJ0Q3qngITn7Vs/7T6li2QZeGAbsYgCeJ3SJ
aLW5MCjK7lRPeC87PKRLEsA=
=eITH
-----END PGP SIGNATURE-----

Jul 19 '05 #6

"Steffen Glückselig" <us****@gungfu.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
1.0 + 3.0 + 4.6 8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?

str(1.0+4.6+3.0)

'8.6'

See Lib Ref 2, Builtin functs, repr() and str(), Lan Ref (or tutorial)
section on % formating, and probably a few FAQ entries.

Terry J. Reedy

Jul 19 '05 #7
Ron Adam wrote:
tiissa wrote:
Steffen Glückselig wrote:

>>1.0 + 3.0 + 4.6

8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)

You may find annex B of the python tutorial an interesting read:
http://docs.python.org/tut/node16.html

In addition to what you find in the above link, the round function can
be used.

p = 1 #digits of precision after decimal
a, b, c = 1.0, 3.05, 4.6
print round(a+b+c,p)

-> 8.6

You also have the option to use the print statements '%' operator to
format the output.
a, b, c = 1.0, 3.05, 4.6
print "%.1f"%(a+b+c)

-> 8.6
_Ron


Just to clarify this, It was pointed out to me that round() doesn't
resolve the problem of floating points, and I agree. (Thanks Fredrik)
round(1.0+3.05+4.6,1) 8.5999999999999996

The print statement displays it as 8.6.

The only way to avoid the problem completely is by using an alternative
numeric system such as decimal.

What rounding and print formatting do is give you some control within
the specifications of your requirements. Print for display purposes,
and round to minimize errors to within the needed precision.
for y in range(1000000): .... x += 1.6
.... x 1600001.6000213262

Eventually this could be significant.
for y in range(1000000): .... x = round(x+1.6,1)
.... x

3200001.6000000001

Here the error has been kept to a minimum. In most cases, it isn't a
problem, but it is something to be aware of. It does matter in banking
and I beleive there are standard ways of dealing with it.

Cheers,
_Ron
Jul 19 '05 #8
On 5/15/05, Ron Adam <rr*@ronadam.com> wrote:

>>> x

3200001.6000000001

Here the error has been kept to a minimum. In most cases, it isn't a
problem, but it is something to be aware of. It does matter in banking
and I beleive there are standard ways of dealing with it.


Yes, use Decimal:

http://docs.python.org/lib/module-decimal.html

Remember that you can also use it in Python 2.3.x:

http://www.taniquetil.com.ar/facundo...t_decimal.html

.. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Jul 19 '05 #9
So for using the python-interpreter as a simple calculator using
'print' seems to be the simplest and still exact way...
Thanks for clarification
Steffen

Jul 19 '05 #10

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

Similar topics

2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
2
by: mdeaver2003 | last post by:
I'm trying to output a double using a precision that varies, governed by the value of a precision variable. In C I can do it like this: double pi = 3.14159; int prec = 4; printf( "%.*f",...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
2
by: rupert | last post by:
i've got the following code: #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; int main(double argc, char* argv) { double r = 0.01;
9
by: asdf | last post by:
I want to set the computation precision to quadruple precision, how can I do it in C++ coding? Thanks all.
10
by: Bo Yang | last post by:
Hi, I am confused by the double type in C++. I don't know whether below is legal or possible: double PI = 3.141592675932; Now, can I get another double variable from PI with lower precision,...
6
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
0
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.