2.6.1 - simple division

F

farsight

4 / 5.0
0.8000000000000000044.0

python 2.6.1 on mac. What the hell is going on here?
 
F

farsight

0.800000000000000004>>> 0.8 * 5

4.0

python 2.6.1 on mac. What the hell is going on here?

Pure curiosity prompted me to try the following:8.0

Strange...
 
L

Lie Ryan

This one is a common FAQ. Basically floating point is never to be
trusted. This issue is quite language agnostic, however some language
decided to "hide" the issue, python does not. For more information on
floating point and its intricacies:
http://docs.python.org/tutorial/floatingpoint.html
Pure curiosity prompted me to try the following:
8.0

Strange...

Strange, I don't see anything strange with that...

Perhaps you meant, python returns 4.0 instead of 4? It's because in
division with at least one of the divisor or the dividend a floating
point will return a floating point value.

Perhaps you're confusing it with integer division, in which both divisor
and dividend are integers. In python 2.6, this will still return
integers, but this will change (edit: have changed) in python 3.x,
division of integer by integer will always result in floating point even
if the result can be represented exactly by an integer. You can do 'from
__future__ import division' to use the new division semantic in python 2.x
 
F

farsight

Thanks all, that's very helpful, sorry to waste your time with a
common question. I have tried the decimal module and will definitely
keep using it if I need to do this kind of calculation again.

I have 1 more question that the floating point article that was linked
didn't really answer:
4.0

Shouldn't I be expecting something like 4.0000000000002 ?
 
F

farsight

Thanks duncan, thats very helpful. I'll be more careful with floating
point numbers in future.
 
L

Lie Ryan

Duncan said:
Try to remember though that the decimal module simply replaces one source
of inaccuracies with another one:

1.0

Sometimes you want floating point, sometimes you want Decimal. You need to
understand the advantages and drawbacks of each in order to make an
informed choice.


You should certainly expect that the final result may be a little bit away
from the 'exact' result but rounding errors can work in your favour just as
well as they work against.

You should also keep fractions.Fraction (Rational) number in your
toolbox. Rational is the most accurate, though it still can't represent
irrational numbers[1] (e.g. pi, e, phi, sqrt(2)) and is generally slower
than the others.

[1] actually, neither float nor Decimal can represent irrationals.

PS: Actually, I've been thinking about reducing numbers that can't be
represented as float, Decimals, Rationals by adding such things as surds
and special constants. Basically getting python to have exact arithmetic
library, but I think it'll be too large and complex while not many
people really need exact arithmetic.
 
T

Tim Rowe

2009/3/8 said:
4.0

python 2.6.1 on mac. What the hell is going on here?

I know this has already been answered in detail, but one thing that
it's easy for those new to floating point issues to miss is that
fractions that can be expressed exactly in decimal can end up as
recurring decimals in binary. 0.8 looks nice and tidy, but in binary
(if I get this right) it's 0.1100110011001100..., recurring ad
infinitum. The computer has to truncate it somewhere.
 
S

Steve Holden

Thanks all, that's very helpful, sorry to waste your time with a
common question. I have tried the decimal module and will definitely
keep using it if I need to do this kind of calculation again.

I have 1 more question that the floating point article that was linked
didn't really answer:

4.0

Shouldn't I be expecting something like 4.0000000000002 ?

How different is 4.0?

regards
Steve
 

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
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top