Numerical closeness depends on a stated tolerance, not on identical displayed digits

Two numbers can be different while still being close enough for a particular numerical purpose. Deciding that they are close requires a rule. A tolerance makes that rule explicit by defining how much difference is allowed.

This does not turn unequal values into exactly equal values. It produces a decision under chosen conditions. A useful explanation therefore states the tolerance, the reference value and the units, rather than describing closeness as a property that exists independently of the task.

An absolute tolerance defines a fixed allowed difference

Suppose the reference value is ten and the allowed absolute difference is zero point one. A value of ten point zero six differs from the reference by zero point zero six. It falls within that stated tolerance.

A value of ten point two differs by zero point two and falls outside it. The comparison concerns the distance between numbers, not their written length or the number of digits shown on a screen.

Two differences compared with an absolute allowance of zero point one

If only an absolute tolerance of zero point one is used, the allowed difference stays the same around a reference of one or a reference of one thousand. Whether that fixed allowance makes sense depends on the scale and meaning of the quantity being compared.

A relative tolerance scales with a reference

A relative tolerance expresses part of the allowance in proportion to a reference magnitude. With a relative tolerance of one percent and a reference of one hundred, the relative allowance is one. With a reference of ten, it is zero point one.

This approach can suit comparisons where an acceptable difference should grow with the size of the reference. It does not remove the need for a domain rule. One percent may be appropriate for one quantity and inappropriate for another.

An absolute component can be combined with a relative component. In NumPy isclose, finite values are compared by asking whether the absolute difference is at most the absolute tolerance plus the relative tolerance multiplied by the absolute reference value.

The reference is part of the formula

For NumPy isclose, the second argument supplies the reference in the relative component. That detail matters because the formula is not generally symmetric. Reversing the two arguments can change the allowed difference and therefore change the decision.

Consider ninety and one hundred with a relative tolerance of one tenth and an absolute tolerance of zero. Using one hundred as the reference allows a difference of ten. Using ninety as the reference allows a difference of nine. The actual difference between the two numbers remains ten.

Under the first arrangement, the difference meets the allowance. Under the second, it exceeds the allowance. Neither arrangement should be described without naming the reference convention. A different closeness function may use a different rule, so its documentation must be read rather than assumed.

Near zero, the absolute component needs special attention

When the reference is zero, the relative component in this formula is also zero. The absolute tolerance then determines the entire allowance. A nonzero number may consequently be accepted as close to zero under that rule.

This is not a claim that the number has become zero. It means its distance from zero is within the chosen absolute allowance. If the numbers represent very small but meaningful quantities, a default allowance can hide differences that matter to the application.

The units must also be consistent. An absolute tolerance has the same units as the compared quantity, while the relative tolerance is a proportion. Changing the unit of measurement requires a corresponding change to the absolute tolerance if the intended physical allowance is to remain the same.

A comparison result is not a correction of the inputs

A closeness comparison returns a decision for each compared pair. It does not round the inputs, replace one with the other or reconcile their sources. Those would be separate operations with separate consequences.

For arrays, the pairing of elements must also be correct. Values from different dates or different instruments should not be compared merely because their array positions happen to line up. Shape compatibility alone does not establish that the pairs have the intended meaning.

A clear numerical workflow records the reference, both tolerance components and the units. It also keeps exact equality separate from acceptance within a tolerance. With those conditions visible, closeness becomes a reproducible comparison rule rather than an unexplained judgment about displayed numbers.

Academia Master Trader shares educational material about numerical operations, data structures and result interpretation at academiamastertrader.com.br.

Aprenda