Correctness begins with a specification
A computation is correct when its output satisfies the task’s requirements for the accepted input. Sometimes that means exact equality, as with counting records. Sometimes the task explicitly allows an approximation. A tolerance is meaningful only when it expresses that allowed error, rather than excusing an unexplained mismatch.
For example, sorting a list of integer identifiers requires the same elements in the specified order. Returning almost the same identifiers would not be acceptable. Estimating a real-valued physical quantity may involve measurement uncertainty and numerical approximation. The correct checking rule depends on the problem, not simply on whether the implementation uses a fast method.
Why floating-point results can differ
Finite floating-point formats cannot represent every real number exactly. Many decimal fractions are approximated in binary, and arithmetic can introduce rounding. Python’s floating-point tutorial explains this representation issue. A display that rounds two values to the same decimal text does not prove their stored values are identical.
Order can matter too. Adding a very small quantity to a much larger one may lose information at a chosen precision. Reorganizing a sum can change its rounded result. This does not make every difference harmless; it explains why numerical algorithms need error analysis rather than an assumption that all mathematically equivalent expressions behave identically in finite arithmetic.
Work through absolute and relative error
Suppose a reference value is one hundred and a candidate returns one hundred point zero zero one. The absolute difference is one thousandth. Relative to the reference, the difference is one hundred-thousandth, or 0.001 percent. These are two descriptions of the same illustrative discrepancy.
At a reference value near zero, a relative-only rule becomes difficult to use: a tiny absolute difference can be large compared with a tiny reference. An absolute tolerance expresses a floor in the quantity’s units. A relative tolerance scales with magnitude. Neither choice should be made without considering the task and the expected numerical errors.
Python’s math.isclose reference documents one combined rule: compare the absolute difference with the larger of an absolute tolerance and a relative allowance scaled by the larger input magnitude. For finite values, that rule can be written as:
abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
Choose nonnegative tolerances appropriate to the task. Python treats NaN as not close to any value, and an infinity as close only to the same infinity. Other libraries use different comparison conventions, so record the rule actually used.
Use an independent check
An implementation that verifies its answer by calling itself again may reproduce the same mistake. A better oracle is independently derived: a known exact solution, a simpler trusted method on bounded inputs, or a higher-precision calculation with appropriate analysis. Include structural checks as well as numeric checks.
For a computed probability distribution, entries might need to be nonnegative and sum close to one under the defined tolerance. For a matrix transformation, check dimensions and known special cases. For an exact count, test empty input and boundary values. Handle non-finite values explicitly; a comparison that accidentally accepts a NaN is not a useful correctness test.
Choose the threshold before comparing speed
If a faster candidate differs from the reference, investigate the source and its consequences. Do not repeatedly widen the tolerance until the candidate passes. Define tolerances from the problem, precision, and intended use, then test representative and adversarial inputs. Report the largest observed error and the tested domain rather than only a pass label.
Also distinguish a valid answer from a valid request. A checker may confirm that an output is mathematically consistent with a truncated input while the application was required to process the full input. Preserve input length, type, and identity through the test so a successful numerical comparison cannot hide an upstream change to the problem.
A benchmark should compare implementations that meet the same accepted output contract. If approximation changes that contract, disclose it as a separate quality-and-resource tradeoff. Correctness is the foundation that makes a speed comparison useful. Read floating-point arithmetic for representation details and fair benchmarks for designing comparisons after the checking rule is settled.