Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python float to int conversion

Basically, I'm converting a float to an int, but I don't always have the expected value.

Here's the code I'm executing:

x = 2.51

print("--------- 251.0") y = 251.0 print(y) print(int(y))  print("--------- 2.51 * 100") y = x * 100 print(y) print(int(y))  print("--------- 2.51 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y))  print("--------- 2.51 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y))  x = 4.02 print("--------- 402.0") y = 402.0 print(y) print(int(y))  print("--------- 4.02 * 100") y = x * 100 print(y) print(int(y))  print("--------- 4.02 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y))  print("--------- 4.02 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y)) 

And here's the result (first value is the result of the operation, second value is int() of the same operation):

--------- 251.0 251.0 251 --------- 2.51 * 100 251.0 250 --------- 2.51 * 1000 / 10 251.0 251 --------- 2.51 * 100 * 10 / 10 251.0 250 --------- 402.0 402.0 402 --------- 4.02 * 100 402.0 401 --------- 4.02 * 1000 / 10 402.0 401 --------- 4.02 * 100 * 10 / 10 402.0 401 

2.51 and 4.02 are the only values that lead to that strange behaviour on the 2.50 -> 5.00 range. Every other two digits value in that range converts to int without any problem when given the same operations.

So, what am I missing that leads to those results? I'm using Python 2.7.2 by the way.

like image 366
B. Richard Avatar asked Jul 04 '11 09:07

B. Richard


People also ask

Can we convert float to int in Python?

Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 .

Can I convert a float to an int?

Since a float is bigger than int, you can convert a float to an int by simply down-casting it e.g. (int) 4.0f will give you integer 4. By the way, you must remember that typecasting just get rid of anything after the decimal point, they don't perform any rounding or flooring operation on the value.

How do I change Dtype from float to int?

To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype() to convert. Use DataFrame. fillna() to replace the NaN values with integer value zero.

How does the int function convert a float to an int?

How does the int function convert a float to an int? By rounding to the nearest whole number.


2 Answers

2.51 * 100 = 250.999999999997 

The int() function simply truncates the number at the decimal point, giving 250. Use

int(round(2.51*100))  

to get 251 as an integer. In general, floating point numbers cannot be represented exactly. One should therefore be careful of round-off errors. As mentioned, this is not a Python-specific problem. It's a recurring problem in all computer languages.

like image 105
Pascal Bugnion Avatar answered Sep 23 '22 14:09

Pascal Bugnion


What Every Computer Scientist Should Know About Floating-Point Arithmetic

Floating-point numbers cannot represent all the numbers. In particular, 2.51 cannot be represented by a floating-point number, and is represented by a number very close to it:

>>> print "%.16f" % 2.51 2.5099999999999998 >>> 2.51*100 250.99999999999997 >>> 4.02*100 401.99999999999994 

If you use int, which truncates the numbers, you get:

250 401 

Have a look at the Decimal type.

like image 26
Jacob Avatar answered Sep 25 '22 14:09

Jacob