This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Below code's output different result , while in daily mathematics it results same
d=1.11-1.10
e=2.11-2.10
print('d= ', d ,'e= ' , e)
you are using floating point mathematics, which has limited accuracy. The results are expected!
If you want accurate results, you could use decimal arithmetics:
from decimal import Decimal
Decimal('1.11') - Decimal('1.10')
Decimal('2.11') - Decimal('2.10')
Related
This question already has answers here:
Print pi to a number of decimal places
(8 answers)
Closed 2 years ago.
I am trying to get pi value in python. Here is my source code.
import math
pi_formatted_float = "{:.5000f}".format(math.pi)
print(pi_formatted_float)
Using the source code I only can get 48 decimal places. Others are only 00000000....
More simply, you can test with
>>> "{:.60f}".format(1/3)
'0.333333333333333314829616256247390992939472198486328125000000'
It's not just a problem of PI, but common in all float type. You may find more information from Limiting floats to two decimal points.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I run the following code:
numero = 80.12
a = int(numero)
b = abs(numero) - abs(int(numero))
print(a,b)
And as a result I get...
80, 0.12000000000000455
I don't know what could be the problem of printing excess of decimal places.
The full gory details can be found in this documentation. Floating point representations of decimal fractions are almost never precise.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am using python 3.6.8 and tried to multiply 0.1235 with 10 and the answer is 1.2349999999999999 rather than 1.235.
After importing the decimal module, when we multiply decimal.Decimal(0.1235) with 10 we get Decimal('1.234999999999999986677323704') rather than Decimal('1.235').
So How to do precision float calculations with python?
The value 0.1235 is a decimal fraction that needs to be converted to a binary fraction in memory, resulting in not a 100 percent accuracy. please refer to Floating Point Arithmetic: Issues and Limitations
This question already has answers here:
Is floating point math broken?
(31 answers)
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 4 years ago.
I have some strange result in my python 3.6.3. Once i tried some code and i reach this problem.
>>> a = 10**32
>>> print(a/1000/1000)
9.999999999999999e+25
As you see it not actually right, but if i go other way, i reach what i expect
>>> print(a/1000000)
1e+26
Same thing with
>>> 10**26
>>> 10**31
Can somebody explain me what's wrong? i tried write it in one line no result
>>> a = 10**32
>>> a/1000/1000
9.999999999999999e+25
As you know, Python 3 division is no longer an integer division (a//1000//1000 would have worked fine), so you're performing 2 floating point divisions here, introducing an (unnecessary) floating point accumulation error.
>>> a/1000000
1e+26
this only performs one division, so lower floating point error effect, even if result is now floating point.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
When I divide 1/5e-5 it gives the correct answer but 1/5e-6 gives an answer close to the correct one.
>>> 1/5e-5
20000.0
>>> 1/5e-6
199999.99999999997 (should be 200000)
>>>
How can I get it to show the exact value. I tried importing decimel, numpy and scipy but none of them change the result.
You can get decimal to do this properly -- You just have to avoid using floating point numbers along the way:
>>> import decimal
>>> decimal.Decimal('1') / (decimal.Decimal('5') / decimal.Decimal('1000000'))
Decimal('2E+5')