limited precision of floats in Python [duplicate] - python

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am running this code on Python (both 2.7, 3.x):
>>> 1.1 + 2.2
3.3000000000000003
>>> 1.1 + 2.3
3.4
Could someone explain how does it work and what is happening?

float in Python implementing double point precision. Unless a number has power two denominator, it cannot be represented exactly by Python, but only "approximately" - up to the 16-th digit. Thus number like: 1, 0.5, 0.25 can be represented exactly, but number like your case (3.3) can only be represented "approximately". Its all correct, up to the 16 digit, and then you get the last 3 there, which is incorrect.

Related

Why does math.floor() sometimes round up? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 months ago.
It seems, from what I can tell, that python 3.10.4 math.floor() will sometimes round up instead of down. This seems to be in contrast to the purpose of the function.
Could someone please explain this?
Example:
>>> math.floor(0.9999999999999999)
0
>>> math.floor(0.99999999999999999)
1
floor() has nothing to do with this:
>>> 0.9999999999999999
0.9999999999999999
>>> 0.99999999999999999
1.0
That is, your second literal rounds up to 1.0 all on its own before math.floor() happens. Thus, math.floor() is flooring the number 1, not 0.99999999999999999. Floating-point literals are automatically converted to internal machine binary floating-point format, which has only 53 bits of precision ("IEEE 754 double" format on almost all machines).

Why float numbers (when typed in shell) has an additional digits (zeros) [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm new in python and I was typing float numbers in python2.7 shell when I figured this:
>>> 9.9
9.9000000000000004
>>> 9.9==_
True
>>> 9.9==9.90000004
False
>>> 7.7
7.7000000000000002
>>> 7.7==_
True
>>> 7.7==7.700000002
False
my question is, why does 9.9 became 9.900000000000004? Is it default?
Please let me know if this is a duplicate. thanks.
Generally it is due to the binary nature of the computers. This is not a Python specific issue. The fractional part cannot always be exactly represented in binary numbers. Read about IEEE 754 and check examples&solid explanation here.

Sum of floats: unexpected result [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
The following output surprised me:
1.1 + 2.2
=> 3.3000000000000003
An unexpected small digit came up from the sum. The same does not happen for other addends, e.g.:
3.0 + 0.3
=> 3.3
I tried both in Python 2.7 and in 3.4, but the result is the same. What is the reason for this unexpected result of the sum?
Mainly because binary doesn't play well with decimal (2 and 10 are coprime) and floats have limited precision.
Ultimately, when it comes down to it, computers are working with binary numbers. Some fractional numbers do not translate as neat as we would like to binary numbers. The resulting value includes some left-over digital garbage.
For a more complete discussion, see: python floating number and Limiting floats to two decimal points but a reasonable solution might be to specify the desired precision like:
>>> a = 1.1 + 2.2
>>> a = round(a,1)
>>> a
3.3

How to round a float up on 5 [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 7 years ago.
So I was surprised I didn't find anything regarding this.
I have a python script which is testing a C++ program. It needs to format a float in the same way std::setprecision does. That is a float like 1.265 should be rounded UP to 1.27 (2 dp).
Now I have the following code:
"{:.2f}".format(myFloat)
The issue is that numbers like 1.265 are rounded to 1.26 and my tests fail. setprecision rounds 1.265 to 1.27.
What is the best way to fix this issue?
You can use double rounding to overcome the inability of binary arithmetic to exactly represent a decimal value.
round(round(1.265, 3) + 0.0005, 2)

Python float numbers representation [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Floating Point Limitations
Hi there,
maybe this has a very simple answer. I was playing around with the Python interpreter.
>>> 1
1
>>> 1.1
1.1000000000000001
>>> 1.2
1.2
Why 1.1 was interpreted as 1.1000000000000001?
From The Floating-Point Guide:
Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and
instead I get a weird result like
0.30000000000000004?
Because internally, computers use a
format (binary floating-point) that
cannot accurately represent a number
like 0.1, 0.2 or 0.3 at all.
When the code is compiled or
interpreted, your “0.1” is already
rounded to the nearest number in that
format, which results in a small
rounding error even before the
calculation happens.
That is due to the way the numbers are stored internally. The format is specified in the IEEE_754-2008 speficiation.
Some more information can be found on single precision floats here
you can look at here for Floating Point Arithmetic Issues and Limitations
In any case in python 3 you have:
>>> 1
1
>>> 1.1
1.1
>>> 1.2
1.2
>>>

Categories