Convert Python to int int

Basically, I convert float to int, but I don't always have the expected value.

Here is the code that I am 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 is the result (the first value is the result of the operation, the 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 strange behavior in the range 2.50 → 5.00. Each other value of two digits in this range is converted to int without any problems when performing the same operations.

So what am I missing that leads to these results? By the way, I am using Python 2.7.2.

+60
python floating-point int
Jul 04 2018-11-11T00:
source share
5 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. Therefore, you should be careful with rounding errors. As already mentioned, this is not a Python-specific issue. This is a recurring problem in all computer languages.

+76
Jul 04 2018-11-11T00:
source share

What Every Computer Scientist Should Know About Floating-Point Arithmetic

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

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

If you use an int that truncates numbers, you get:

 250 401 

Look at the Decimal type.

+31
Jul 04 2018-11-11T00:
source share

Languages ​​that use binary floating-point representations (Python is one) cannot represent all fractional values ​​exactly. If the result of your calculation is 250.99999999999 (and this may be), then taking the integer part will result in 250.

Canonical article on this topic What every computer scientist should know about floating point arithmetic .

+10
Jul 04 2018-11-11T00:
source share
 >>> x = 2.51 >>> x*100 250.99999999999997 

floating point numbers are inaccurate. in this case, it’s 250.9999999999999999, which is really close to 251, but int () truncates the decimal part, in this case 250.

you should take a look at the Decimal module, or maybe if you need to calculate a lot in the mpmath library http://code.google.com/p/mpmath/ :),

+7
Jul 04 2018-11-11T00:
source share

int converted by truncation, as mentioned by others. This can lead to the fact that the answer will differ from the expected one. One way to solve this problem is to check whether the result is "close enough" to an integer and, accordingly, adjust, otherwise, a normal conversion. This is provided that you do not get too much rounding and calculation errors, which is a separate problem. For example:

 def toint(f): trunc = int(f) diff = f - trunc # trunc is one too low if abs(f - trunc - 1) < 0.00001: return trunc + 1 # trunc is one too high if abs(f - trunc + 1) < 0.00001: return trunc - 1 # trunc is the right value return trunc 

This function will correct errors that occur when approaching integers. The mpmath library does something similar for floating point numbers close to integers.

+1
Oct 13 '18 at 18:37
source share



All Articles