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
Related
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
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 6 years ago.
I was writing a Python 3.5.2 program which includes division.
When I ran the following:
>>> 10/2
5.0
Though the answer is correct, it has a decimal point. What should be done to get quotient without decimal point, for example, 10/2 should yield 5, not 5.0?
Check out this answer. In Python 3 Integer division is performed using the // operator, and the regular division operator can yield float. Check 1/2 for example - if you get 0.5, that's not Integer division, 0 is. IDLE is irrelevant here.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I was trying to calculate int((226553150*1023473145)/5.) using python and I got 46374212988031352 although it should be 46374212988031350.
Remove the period for integer division:
int((226553150*1023473145)/5)
Comes out as 46374212988031350 for me.
Edit after #tobias_k's comment:
This only works in Python2, where / is either floating point division (if either of the arguments is float) or integer division (if both arguments are integers).
In Python3 (or with from __future__ import division in Python2) / is always floating point division and the rounding problem comes up again, because the numbers you multiply are too large to be exactly expressed in floating point.
The solution is either to use //, which is always integer division in all Python versions or (if you really need the denominator to be float) to switch to mpmath, where you can increase the floating point precision until you don't have the rounding errors anymore.
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 8 years ago.
I'm trying to have this function work, but i'm not getting the results expected. I fully believe it because I'm new to Python.... Here is my code:
waterLevel = (-(25/144) * sensorValue) + (14725/72) ;
And here is the equation:
y=-25x/144 + 14725/72
So when x=1178 then y=0 (y=waterLevel) and when x=602 then y=100.
I am getting this, when x=1178 then y=204.
What am I doing wrong here?
Update This is a different question because I didn't know it was floating point python nonsense .... just that my math wasn't working. Just because the answer is the same doesn't mean it is a duplicate. ;)
You are falling foul of integer division as opposed to float division.
In Python 2.x 25/144 will produce integer division which means it will result in 0.
If you're going to be dealing with floating point numbers then you should use floating point numbers, i.e. 25.0/144.0.
In Python 3.x the division of integers will default to returning the floating point value and instead you must explicitly choose integer division using //. If you would like this behaviour in Python 2 then use from __future__ import division at the top of your script (thanks to jwodder for the comment).
Use floats instead of ints
waterLevel = (-(25.0/144.0) * sensorValue) + (14725.0/72.0)
By the way, there is an adicional parethesis in the beginning of the equation, is it a typo?