Python round: ties go to the even number
round(x) returns an integer. When x is exactly halfway between two integers, round picks the even one. This run shows the same result. round(x, n) rounds to n decimal places; with a float x the result is a float.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-104-A: round on halfway integers goes to the even number, round with digits on a float returns a float, and 2.675 rounds to 2.67
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
print(round(0.5), round(1.5), round(2.5), round(3.5))
print(round(7.456, 1), type(round(7.456, 1)).__name__)
print(type(round(7.6)).__name__)
print(round(2.675, 2))
EOSCommand:
python3 s.pyRecorded output:
0 2 2 4
7.5 float
int
2.67Exit status: 0. This is the status recorded in the log for TE-104-A.
TE-104-B: round on negative halfway values, with a negative number of digits, and with 0 digits on an integer
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
print(round(-1.5), round(-2.5), round(1234, -2), round(5, 0))
EOSCommand:
python3 s.pyRecorded output:
-2 -2 1200 5Exit status: 0. This status and the output match what was written before the run.
TE-104-C: expected non-zero exit: round of a string raises TypeError
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
round('x')
EOSCommand:
python3 s.pyRecorded output: none, standard output was empty.
Standard error from the same run:
Traceback (most recent call last):
File "/tmp/b26-kwjy0h5m/work/s.py", line 1, in <module>
round('x')
TypeError: type str doesn't define __round__ methodExit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
Reading the recorded output
The first line shows round applied to 0.5, 1.5, 2.5 and 3.5. It printed 0, 2, 2 and 4. All four values are halfway between two integers, and each went to the even neighbor: 0.5 to 0, 1.5 to 2, 2.5 to 2, 3.5 to 4. The second line shows that round(7.456, 1) is 7.5 and that its type is float. The third line shows that round(7.6) has the type int. The last line shows that round(2.675, 2) printed 2.67, not 2.68.
TE-104-B: round(-1.5) and round(-2.5) both gave -2, so negative halfway values also go to the even neighbor. round(1234, -2) gave 1200, and round(5, 0) gave 5. TE-104-C: round('x') stopped with exit status 1. The last line of standard error is TypeError: type str doesn't define __round__ method.
Limits
The run uses a few fixed values. The docs (https://docs.python.org/3/library/functions.html#round) say: round(2.675, 2) gives 2.67 instead of the expected 2.68. This run shows the same result. TE-104-A runs float and halfway-integer inputs. TE-104-B runs an integer x with a negative n.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-104-A | round on halfway integers goes to the even number, round with digits on a float returns a float, and 2.675 rounds to 2.67 | 0 | PASS | f1b03923c5e9 | | TE-104-B | round on negative halfway values, with a negative number of digits, and with 0 digits on an integer | 0 | PASS | 32731b2dad87 | | TE-104-C | expected non-zero exit: round of a string raises TypeError | 1 | PASS | ea381189e5c0 |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides