Python round(), floor(), ceil() and int() With Negative Numbers, Measured
What Python’s round, math.floor, math.ceil, int and math.trunc return for halves and negative numbers, next to JavaScript’s Math.round, Math.floor, Math.ceil and Math.trunc — measured on our own computer.
What we measured: Linux x86_64 (kernel 7.0.0-34-generic), CPython 3.12.13 and CPython 3.14.4, Node.js v20.20.2, on the 11 inputs below.
Not measured: Windows and macOS, browser JavaScript engines, PyPy, other Python and Node.js versions, other languages, and round with a number of digits.
What the measurement shows
- Python’s
round(x)sent each of the 6 halves in this measurement to the even neighbour: 2.5 →2, -2.5 →-2, 3.5 →4, -3.5 →-4, 0.5 →0, -0.5 →0. - JavaScript’s
Math.round(x)sent each of the same 6 halves up, toward positive infinity: 2.5 →3, -2.5 →-2, 3.5 →4, -3.5 →-3, 0.5 →1, -0.5 →-0. round(x)andMath.round(x): a different value for 3 of the 11 inputs (2.5, -3.5, 0.5); the same value but a different sign of zero for 2 (-0.5: Python0, JavaScript-0; -0.4: Python0, JavaScript-0); the same value and sign for 6.math.floor(x)andMath.floor(x): the same value for all 11 inputs.math.ceil(x)andMath.ceil(x): the same value for all 11 inputs; the sign of zero differs for -0.5 (Python0, JavaScript-0), -0.4 (Python0, JavaScript-0).math.trunc(x)andMath.trunc(x): the same value for all 11 inputs; the sign of zero differs for -0.5 (Python0, JavaScript-0), -0.4 (Python0, JavaScript-0).int(x)gave the same result asmath.trunc(x)for all 11 inputs, which is not alwaysmath.floor(x):int(-2.5)is-2whilemath.floor(-2.5)is-3;int(-3.5)is-3whilemath.floor(-3.5)is-4;int(-0.5)is0whilemath.floor(-0.5)is-1;int(-2.7)is-2whilemath.floor(-2.7)is-3;int(-0.4)is0whilemath.floor(-0.4)is-1.x // 1gave the same value asmath.floor(x)for every input, but as a float for the 9 inputs written with a decimal point (for example-3.0) and as an int for 7 and -7.
Measured results
Each value is what the interpreter printed: Python shows repr() of the result, JavaScript shows the result with negative zero written as -0.
Both CPython versions returned the same value in every cell, so the Python columns are shown once.
Rounding: round(x) and Math.round(x)
| x | Python round(x) | JavaScript Math.round(x) |
|---|---|---|
2.5 | 2 | 3 |
-2.5 | -2 | -2 |
3.5 | 4 | 4 |
-3.5 | -4 | -3 |
0.5 | 0 | 1 |
-0.5 | 0 | -0 |
2.7 | 3 | 3 |
-2.7 | -3 | -3 |
-0.4 | 0 | -0 |
7 | 7 | 7 |
-7 | -7 | -7 |
Python: math.floor, math.ceil, int, math.trunc and x // 1
| x | math.floor(x) | math.ceil(x) | int(x) | math.trunc(x) | x // 1 |
|---|---|---|---|---|---|
2.5 | 2 | 3 | 2 | 2 | 2.0 |
-2.5 | -3 | -2 | -2 | -2 | -3.0 |
3.5 | 3 | 4 | 3 | 3 | 3.0 |
-3.5 | -4 | -3 | -3 | -3 | -4.0 |
0.5 | 0 | 1 | 0 | 0 | 0.0 |
-0.5 | -1 | 0 | 0 | 0 | -1.0 |
2.7 | 2 | 3 | 2 | 2 | 2.0 |
-2.7 | -3 | -2 | -2 | -2 | -3.0 |
-0.4 | -1 | 0 | 0 | 0 | -1.0 |
7 | 7 | 7 | 7 | 7 | 7 |
-7 | -7 | -7 | -7 | -7 | -7 |
JavaScript: Math.floor, Math.ceil and Math.trunc
| x | Math.floor(x) | Math.ceil(x) | Math.trunc(x) |
|---|---|---|---|
2.5 | 2 | 3 | 2 |
-2.5 | -3 | -2 | -2 |
3.5 | 3 | 4 | 3 |
-3.5 | -4 | -3 | -3 |
0.5 | 0 | 1 | 0 |
-0.5 | -1 | -0 | -0 |
2.7 | 2 | 3 | 2 |
-2.7 | -3 | -2 | -2 |
-0.4 | -1 | -0 | -0 |
7 | 7 | 7 | 7 |
-7 | -7 | -7 | -7 |
Further reading
The rules are defined in the Python documentation for round, the math module documentation and the ECMAScript specification for Math.round; we link to them and do not quote them.