Finite representations have limits
A computer number has a representation. A binary floating-point value uses a finite amount of information to describe a significand and scale. Many decimal fractions cannot be represented exactly in that system, much as one third cannot be written as a finite decimal expansion.
Python’s floating-point tutorial explains this representation issue and distinguishes the stored binary value from its rounded decimal display. The effect is not unique to Python. Floating-point limitations.
Try a small original rounding model
To see the idea without depending on a particular machine, imagine a toy decimal system that keeps only three significant digits after every operation. It can store 1.23, but 1.234 must be rounded. If we use nearest rounding, it becomes 1.23.
Now calculate 100 plus 0.123. The exact result is 100.123, which our toy system rounds back to 100. Subtract 100 afterward and the result is zero. If we first subtract 100 from 100 and then add 0.123, the result is 0.123. The same real-number algebra has different outcomes because the intermediate rounding differs.
Actual binary formats have different precision and rules, but the lesson is useful: regrouping arithmetic can change rounded results. An optimization that changes the order of additions should be evaluated against a numerical contract rather than assumed identical in every bit.
Display precision is a separate choice
An application may show two decimal places while carrying a different value internally. Formatting changes the representation shown to the reader; it does not automatically change every later arithmetic operation. Conversely, rounding the stored value at every step can introduce additional loss of information.
For a report, decide where rounding belongs. Is a line item rounded before calculating tax, or is the total rounded afterward? That is a domain rule. A programming language’s default printing behavior should not silently choose it for the business process.
Equality needs a declared meaning
Sometimes exact comparison is correct, such as comparing fixed identifiers or a known exact integer calculation. For approximate numerical outputs, a tolerance may be appropriate. An absolute tolerance controls a difference in the units of the result. A relative tolerance scales with magnitude.
For example, a tolerance of 0.01 meters means something different from a relative error of one percent. Near zero, a relative rule alone can be unsuitable. The acceptance criterion should come from the application’s requirements and should be defined before comparing implementations.
Alternative number systems also have contracts
Decimal arithmetic can represent finite decimal inputs exactly when constructed and used appropriately. Python’s decimal documentation describes configurable precision and rounding contexts. It also shows why constructing a decimal from an already rounded binary float preserves that float’s value rather than recovering an intended original string. Decimal arithmetic.
Decimal is not a promise of infinite exactness. Dividing one by three still requires a finite-precision decision unless a rational representation is used. Integers, fixed-point values, rational numbers, and binary or decimal floating point each suit different requirements.
Why this matters for evidence
When two programs produce slightly different numeric outputs, investigate representation, precision, operation order, and the accepted error. Do not call one incorrect merely because its final displayed digit differs; do not call both correct without checking a suitable rule either.
For performance testing, publish the number format and correctness criterion alongside timings. Read vectors and matrices for computations built from many such operations, and research methods for reproducible correctness checks.