Python math output incorrect [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why doesn't this division work in python?
A simple problem I'm having (I Think) The following statement:
print (4950*8)/(((4950*8)/10000000*(1538/1460))+0.1/1000)/1000
Gives me 396000.0.
But on a Calculator I get 9270.614192621.
If someone could point out what I'm doing wrong in the code that would be great.
Thanks.

>>> print (4950.0*8)/(((4950.0*8)/10000000*(1538.0/1460))+0.1/1000)/1000
9270.61419262

Old versions of Python use truncated integer division for int operands.
Try from __future__ import division (see http://www.python.org/dev/peps/pep-0238/ for the full story) or coerce int operands to float (e.g. with float, or by appending .0 to literals).

Related

Dividing large number by 10 in python gives the wrong result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Say I want to remove the last digit of a number n.
For that I use the code int(n/10).
Sadly this gives the wrong result for large numbers.
For example n = 4474630975855204960 divided by 10 gives 447463097585520512.
What's the reason for this behavior? How can I fix it?
For some math operations, the Python interpreter will handle long integers for you and you don't have to think about it.
Division is different and converts integers to floats, which aren't handled well in Python.
You can get around this by directly using integer division - two // rather than just one /.
Input
4474630975855204960//10
Output
447463097585520496
This syntax varies across python versions, use // to get integer division
$ python2 -c 'print("%d" % (4474630975855204960/10))' # Integer division
447463097585520496
$ python3 -c 'print("%d" % (4474630975855204960/10))' # Float division
447463097585520512
$ python3 -c 'print("%d" % (4474630975855204960//10))' # True division
447463097585520496
You can convert from int to str, remove the last character, and convert it back to an int:
n = 4474630975855204960
print(int(str(n)[:-1]))
Output:
447463097585520496

Mathematics and behavior with floats vs ints [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 5 years ago.
This is driving me mad... Of all the years I've been using python, this is just now starting to present itself. How I managed to dodge it up until now is beyond me.
If you open a python idle and try this equation...
4/32*100
You'll get '0' as an answer. Now try the same equation using floats....
4.0/32.0*100.0 (or just the first number 4.0/32*100)
You now get an actual percentage.
WTF!?
Is this some kind of python error!? Even a calculation can do the equation and spit out a percentage.
So why can't python see a 4 as 4.0. Better question... What is the interpreter actually seeing if it's not seeing a 4(4.0)?
Someone please clear this up so I can feel professional with python again (lol).
In Python 2, int type division ignores the decimal values of the division.
For example, 1/2 = 0.5, but in int type division, 1/2 will evaluate to 0 because it ignores the decimal values.
Thus, in your case with 4/32*100, 4/32will first evaluate to 0 and then 0*100 will finally equal 0.
On the other hand, in float type division, it will evaluate answers as we would expect (not in a strictly precise definition though, look here for further information).
For Python 2.x, dividing two integers or longs uses integer division, also known as "floor division"(applying the floor function after division)
For Python 3.x, "/" does "true division" for all types.
To make python perform true division, cast any of the denominator for numerator to become float.
float(4)/32*100
or
4/float(32)*100
or doing below to make python 2 division behave like python 3 division
from __future__ import division
4/32*100

printing a number and writing in ipython interpreter gives different outputs [duplicate]

This question already has answers here:
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 6 years ago.
t is array of two float64 numbers.
On typing t in Ipython 2.7, it is giving following output:
array([ 60.211127, 71.08120185])
print t gives
[ 60.211127, 71.08120185]
print t[0] gives
60.211127
but...
t[0] gives
60.211126999999998
as an output.
P.S.
from decimal import *
Decimal(t[0])
gives
Decimal('60.21112699999999762212610221467912197113037109375')
as output.Why is it happening so?
The issue I think you are having is because there is no way to approximate some values in some data formats. (the same way you can't show 1/3 because you would just have .3333333333333... forever) There is more info here
a useful function might be repr() more info here

Python returns 0 for all my divisions (even when using float()) [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 7 years ago.
I'm using Spyder (Python 2.7)
Any division that would return a value below 1 returns 0.
When I use float(5/10) it returns 0.0
When I use:
'%.11f'%a after defining a = 10/20 it still returns 0.000000000..
I'm really new to Python, I'm sorry if this is a dumb question.
How do I fix this? Thank you
Very noob.
For this to work one must just import:
from __future__ import division

Formula in Python not working as expected [duplicate]

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?

Categories

Resources