Can somebody please explain me why the code below behaves like it does? (It's Python in command line on Windows7 x64)
>>>2.22 + 0.22
2.44000000000000004
>>>(222+22)/100
2.44
Floating point operations are limited in percision, and in python the limitations are well documented. You can read about it here
All floating point math is like this and is based on the IEEE standard.
Floating point oprations are known to cause errors.
http://en.wikipedia.org/wiki/IEEE_floating_point
Use the decimal
module if you want precise calculations.
this is due to the data format.
2.22 + 0.22 != 2.44 // both are float
// when you use them to calculate, they are giving consistently "wrong" results
// because the datatype in itself gets incorrect when moving into deep comma space
(222+22) / 100 // becomes this in calculation
222+22 = 244 --> 244/100 = 2.44
The numbers you are adding are in float format. That means it has decimal places. The second line of maths' numbers are all integers, so they are in integer form. Float form is known to throw errors whilst carrying out maths equations.
来源:https://stackoverflow.com/questions/13266732/python-odd-operation