This question already has answers here:
Is floating point math broken?
(31 answers)
Floating Point Numbers [duplicate]
(7 answers)
Closed 4 years ago.
I am trying to understand the decimal rounding behavior in python. What I find strange is the following:
1) Multiplying 70 by 11.46950 gives me 802.865
2) Multiplying 70 by 11.46750 gives me 802.7249999999999
Why is there extra precision in the second case and not the first case? I understand that internally, the decimal cannot be represented exactly. But that reason should also apply to the first case as well?
I am using python3.6.
Thanks
Related
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 1 year ago.
I've learnt that python supports very large numbers with int itself.
But in this case :
print(int(12630717197566440579/10))
My answer is
1263071719756644096
and not
1263071719756644057
As it's supposed to be.
Can someone tell me why?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I was using a simple for loop to add numbers but I found a strange result when adding float.
Can you explain why I have the following output ?
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001
2.100000000000001
2.200000000000001
2.300000000000001
2.4000000000000012
2.5000000000000013
2.6000000000000014
2.7000000000000015
2.8000000000000016
2.9000000000000017
3.0000000000000018
3.100000000000002
3.200000000000002
3.300000000000002
3.400000000000002
3.500000000000002
3.6000000000000023
3.7000000000000024
3.8000000000000025
3.9000000000000026
This is based on Anaconda Spyder
a = 1
for i in range(1,30):
a = a+0.1
print(a)
It's a known limitation of floating point arithmetic, computers cannot store infinitely precise floating point numbers. See python docs.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
When I subtract 4.7 from 2.3, I get a number with 16 decimal places, instead of getting a specific number with one decimal place. How come it doesn't give you a specific answer?
This is because of the numerical representation of both decimal numbers (4.7 and 2.3) in binary:
4.7 is represented in binary as 100.10110011001100110011...
2.3 is represented in binary as 10.01001100110011001101...
As you can see, both are periodic tithes in binary. This is why you do not obtain a precise result.
I hope it helps.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Why does '0.2 + 0.1' show as '0.30000000000000004'? [duplicate]
(1 answer)
Closed 7 years ago.
Why when I'm doing this simple math subtraction I get this answer ?
In[10]: 1-0.9
Out[10]: 0.09999999999999998
someone know how to fix this ?
Refer https://docs.python.org/2/tutorial/floatingpoint.html
Use round(1-0.9, n) where it rounds the result to n decimal places
This is a common problem with floating point precision. Usually people round when floats are displayed so the precision limitation is not shown.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 9 years ago.
When I do a division in Python/Pandas (e.g. 47/100) how do I show the decimal value of the answer, because at the moment it just shows as 0?
Thanks in advance.
If you're using python2.x, you need to "floatify"1 one of your numbers:
float(47)/100
47.0/100
As python2.x will do integer division if both numbers in the division are integers.
1floatify: Forcing a number to be a float