Printing excess decimal places in python [duplicate] - python

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.

Related

why isn't 0.1235 * 10 equals 1.235 in python? [duplicate]

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

Different division result python [duplicate]

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.

below python code results different output than in real maths [duplicate]

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')

Why did python add 1 at the end? console.log(9.89+3.48) = 13.37000000000001 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
https://i.imgur.com/YYNhvNv.png
Its adding in 1 at the end of that console log output. Why?
Answer is suppose to be only 13.37
Likely because it is adding to numbers of type double (I am not a huge Python expert though, so I could be wrong). The doubletype, much like single does not have perfect precission. Hence some decimal errors can occur...

Python 2.7 division not showing exact result [duplicate]

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')

Categories