pythonpython-2.7floating-pointint

python difference between round and int


I've been playing around with python for a bit now and i've noticed a strange behavior that makes me curious: what is the difference between float(int(n)) and round(n)?

When should I use one, another or neither of them?


Solution

  • Note: The python implementation changed between 2.7 and 3.x. I corrected the answer accordingly.

    For the sake of completeness, let me add two more functions to your question and explain the differences between float(int(x)), math.floor(x), round(x) and math.ceil(x).

    Let's start with a question: "What integer represents best the number 1.6?" We have two possible answers (1 and 2) but many different reasons why one answer may be better than the other one:

    Let's ask python to print a nice table of the results you get with different values of x:

    from math import floor, ceil
    tab='\t' 
    
    print('x \tint\tfloor\tround\tceil')
    for x in (
        1.0, 1.1, 1.5, 1.9, -1.1, -1.5, -1.9, 
        -2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
    ):
        print(x, int(x), floor(x), round(x), ceil(x), sep=tab)
    

    Here is the output:

    x       int floor   round   ceil
    1.0     1   1       1       1
    1.1     1   1       1       2
    1.5     1   1       2       2
    1.9     1   1       2       2
    -1.1    -1  -2      -1      -1
    -1.5    -1  -2      -2      -1
    -1.9    -1  -2      -2      -1
    -2.5    -2  -3      -2      -2
    -1.5    -1  -2      -2      -1
    -0.5    0   -1      0       0
    0.5     0   0       0       1
    1.5     1   1       2       2
    2.5     2   2       2       3
    

    You see that none of these four functions are equivalent.

    Note: The python implementation changed between 2.7 and 3.x: Python 2.7 does not use the "half-to-even rounding" rule (explained above) but rounds all half-numbers away from zero: round(1.5)==2 and round(-1.5)==-2. Bankers and mathematicians who care about this agree that the "half to even rounding" rule used in 3.x is the "right way" to do it because it distributes rounding errors fairly.