Precision in Python
Hi. I'm having a precision problem in python Example:
a = 5.14343434 b = round(a,1) b 5.0999999999999996
It´s possible to round the number exactly to 5.1
Elton Mendes wrote:
Hi. I'm having a precision problem in python
Example:
a = 5.14343434 b = round(a,1) b 5.0999999999999996
It´s possible to round the number exactly to 5.1
Read this: http://www.python.org/infogamifaq/general/whyarefloatingpointcalculatio...
On 11/27/06, Elton Mendes <emgmendes@gmail.com> wrote:
Hi. I'm having a precision problem in python
Example:
a = 5.14343434 b = round(a,1) b 5.0999999999999996
It´s possible to round the number exactly to 5.1
Short answer, no. The number 5.1 can't be exactly represented as a binary fraction, i.e., it can't be expressed in the form int/power_of_two. If all you are worried about is appearance, then the print routine will round it to 5.1 if you restrict the precision of the output. Chuck
participants (3)

Charles R Harris

Elton Mendes

Tim Hochberg