ML Foundations

Choose a rounding rule for half values in Python

Compare round-to-even with an explicit decimal half-up policy using a small reproducible fixture.

Example results for Choose a rounding rule for half values in Python. round: 2, 4, -2
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Halfway does not always round away from zero

When Python rounds a built-in numeric value and the two nearest results are equally close, round chooses the even result. Thus 2.5 becomes 2 and 3.5 becomes 4 when rounding to an integer. The rule also applies to the negative halfway value in the example. Check the intended rule before turning scores into displayed whole numbers. [1]

This is a tie rule for built-in types; custom numeric objects can define their own rounding behavior. It does not imply every decimal-looking float is an exact halfway value. [1]

References: [1], [2]

Compare explicit decimal rounding

Run python examples/python_round_half_policy.py. The first list applies round to three exactly representable half-integer floats. The second constructs Decimal values from strings and quantizes them to whole units with ROUND_HALF_UP. For the negative tie this mode goes away from zero, yielding -3. [1, 2]

The example asserts the two expected sequences. Constructing the decimal values from strings preserves these intended decimal inputs; constructing a Decimal from a float instead captures the exact binary float value, which may contain additional digits. [2]

The printed round(2.675, 2) result illustrates the separate representation issue: the binary float is not exactly the decimal value 2.675. An unexpected result cannot always be explained by the tie-breaking rule alone. [1, 2]

Executed with Python 3.12.14; stdout and checksum are in private evidence.

from decimal import Decimal, ROUND_HALF_UP

values = [2.5, 3.5, -2.5]
builtin = [round(value) for value in values]
text_values = ["2.5", "3.5", "-2.5"]
half_up = [Decimal(value).quantize(Decimal("1"), rounding=ROUND_HALF_UP) for value in text_values]
assert builtin == [2, 4, -2]
assert half_up == [Decimal("3"), Decimal("4"), Decimal("-3")]
print("round:", builtin)
print("half up:", [str(value) for value in half_up])
print("binary float example:", round(2.675, 2))

Recorded output

round: [2, 4, -2]
half up: ['3', '4', '-3']
binary float example: 2.67

References: [1], [2]

Record the contract at the boundary

Choose and document a rule where the application requires rounded output. Keep intermediate values at appropriate precision if later calculations depend on them; repeated rounding can change a result. A display convention and a computation policy need not be identical.

This fixture tests three half integers and one binary float. It does not establish a universal choice of rounding rule, cover all Decimal context limits, or compare execution speed.

References: [1], [2]

Sources

  1. Python built-in round
  2. Python decimal rounding modes