weird multiplication behaviour in Python 2.5 under Kubuntu 8.10

Asked by gedece on 2008-12-07

My system:
 lsb_release -rd
Description: Ubuntu 8.10
Release: 8.10

apt-cache policy python
  Instalados: 2.5.2-1ubuntu1
  Candidato: 2.5.2-1ubuntu1
  Tabla de versión:
 *** 2.5.2-1ubuntu1 0
        500 intrepid/main Packages
        100 /var/lib/dpkg/status

Under Kubuntu 8.10 updated from 8.04 I was trying a program in Python 2.5.2. I have this Python because adept doesn't show 2.6 or 3.0.

this program made a calculation and didn't meet the condition, so I begun testing it in the python command line. To my surprise I got this nice repeatable error.

Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2.15*7

clearly this should have read 15.05

Question information

English Edit question
Ubuntu python-defaults Edit question
No assignee Edit question
Last query:
Last reply:
Florian Diesch (diesch) said : #1

Thank you for taking the time to report this issue and helping to make Ubuntu better.

The reported behavior is a common problem when using floating point values: Due to the limited precision some values can't be expressed precisely, similar to e.g. 1/3 can't be expressed as a decimal value.

As this issue isn't really a bug I'm going to convert it to a question. Feel free to ask further questions if something about it isn't clear.

gedece (gedece) said : #2

I can understant limited precision being a problem in large decimals floating point values. But I'm working with only 2 decimals.

Florian Diesch (diesch) said : #3

Python is using a binary represention for floating point values (see and for details). Some numbers - like 15.05 and 2.15 - can't be represented that way so Python needs to round:

>>> 2.15
>>> 15.05

Python just can't distinguish that numbers:

>>> 15.05 == 15.050000000000001
>>> 2.1499999999999999 == 2.15

That's not a Python specific issue but affects most other programming languages as well.

It also means that you have to be very careful when comparing floating point values for equality as the result may not what you expect.

Python comes with the 'decimal' module that provides decimal floating point arithmetic. It's a bit slower than using native floating point values but may work more lie you expect to:

>>> from decimal import Decimal
>>> Decimal('2.15')*7

But it has its limitations, too:

>>> (Decimal(1)/3)*3

Here native floating point values do more like you would expect:
>>> (1.0/3)*3

Can you help with this problem?

Provide an answer of your own, or ask gedece for more information if necessary.

To post a message you must log in.