Experiments and implementations · Intermediate

Examine cancellation and input rounding

Compare a cancellation-prone expression and its reformulation against a high-precision reference, separating arithmetic error from lost input information.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Inspect information left in a small difference
  2. Reformulate without the cancellation-prone numerator
  3. Run a self-contained reference comparison
  4. Interpret absolute and relative error
  5. Separate input rounding from subtraction error
  6. Label a decimal-input reference as a different question
  7. Solve domain and lost-input exercises
  8. Ask Logeia about this articleCheck your understanding

Inspect information left in a small difference

Evaluate f(x)=(sqrt(1+x)-1)/x near x=10 to the minus sixteen. Subtracting nearby values can expose prior approximation error as a large fraction of the small difference. Read the floating-point representation and numerical-stability guides first. This is not a claim that every small answer is inaccurate. It compares calculation paths against a higher-precision reference for the same stored input, rather than timing a performance optimization.

The program checks that float has the expected 53-bit significand. Decimal.from_float(x) then constructs a reference from the exact stored binary input. Decimal('1e-16') would instead ask about the original decimal input, a legitimate but different comparison. Keeping these separate prevents input rounding from being silently mixed with arithmetic error. The chosen reference boundary is part of the experiment, just as a traffic boundary is part of a performance measurement.

Reformulate without the cancellation-prone numerator

Multiplying by sqrt(1+x)+1 gives the equivalent expression 1/(sqrt(1+x)+1) where the original expression is defined and x is nonzero. The original numerator subtracts close values; the new denominator adds positive values. At the chosen float input, 1+x rounds to one in the expected environment, making the original result zero while the reformulation returns 0.5, close to the reference. This is not merely a choice of displayed digits.

The reformulation still contains rounded square root, addition and division. It is not perfectly accurate for every input. At x=0, the original expression is undefined while the reformulation gives the continuous extension 0.5. An API must decide whether that extension is allowed. Values below minus one also leave the real square-root domain. Check the domain and special-input contract before declaring two machine implementations interchangeable solely because their algebraic expressions are equivalent.

Run a self-contained reference comparison

Save the complete program as cancellation.py and run it with Python 3.12 or a compatible later version. Decimal uses eighty digits to evaluate a reference for the same stored float input. That finite precision has ample margin for this comparison, but it is not an exact symbolic proof. Print ordinary results with repr and compute differences in Decimal so apparently identical short displays do not hide the relevant error.

Assertions require the reformulation's absolute error below 1e-16 and the original expression's error above 0.4 for this input. These are coarse case-specific checks, not global error bounds. A differing environment should be investigated before adjusting thresholds to fit its output. The initial significand assertion makes an important representation assumption executable. No external package or data file is required, and the input remains fixed in the code so a reproduction can begin from the same values and comparison contract.

python
from decimal import Decimal, localcontext
import math, sys
assert sys.float_info.mant_dig == 53
x=1e-16
naive=(math.sqrt(1.0+x)-1.0)/x
stable=1.0/(math.sqrt(1.0+x)+1.0)
with localcontext() as context:
    context.prec=80
    dx=Decimal.from_float(x)
    reference=1/((1+dx).sqrt()+1)
    for name,value in [('naive',naive),('stable',stable)]:
        absolute=abs(Decimal.from_float(value)-reference)
        relative=absolute/abs(reference)
        print(name, 'value=',repr(value),'absolute=',absolute,'relative=',relative)
    assert abs(Decimal.from_float(stable)-reference)<Decimal('1e-16')
    assert abs(Decimal.from_float(naive)-reference)>Decimal('0.4')
rounded=float(10**16+1)
assert rounded==float(10**16)
assert rounded-float(10**16)==0.0
assert Decimal(10**16+1)-Decimal(10**16)==1
print('input_rounding',repr(rounded), 'stored_difference',rounded-float(10**16))

Interpret absolute and relative error

Absolute error is the magnitude of result minus reference. Relative error divides that by the reference magnitude. A zero result against a reference near 0.5 has absolute error near 0.5 and relative error near one, or one hundred percent. A small absolute error can still be a large relative error for a smaller target. At a zero reference, this relative-error definition is undefined; choose an absolute or application-specific criterion rather than divide by zero.

Convert float results with Decimal.from_float before differencing so the exact stored binary value is represented. Converting through a shortened decimal string introduces display rounding into the comparison. Printing more digits cannot recover information already lost during arithmetic. Trace intermediate 1+x separately from the final zero to identify where the approximation entered. Retaining input, intermediate values and final errors under one explicit conversion rule makes that explanation reproducible.

Separate input rounding from subtraction error

The second example converts integer 10,000,000,000,000,001 to float and subtracts 10,000,000,000,000,000. Both become the same stored float, so their difference is zero. Decimal values constructed directly from the integers differ by one. The key information loss happened during conversion before subtraction. Do not conclude that the subtraction operation itself must be inaccurately rounded; subtraction of nearby stored floats can be exact under appropriate conditions.

Replacing only the final subtraction with higher precision cannot reconstruct an input distinction already discarded. A higher-precision design must specify how original input is parsed and where that precision is retained. Integer units can be useful for some decimal quantities, but need explicit range and fractional-unit rules. This is not a money-processing implementation. It illustrates why error boundaries belong at both data conversion and subsequent computation, rather than only beside the final arithmetic operator.

Label a decimal-input reference as a different question

An additional reference constructed from Decimal('1e-16') evaluates the original decimal input rather than the exact stored float. Its difference includes input conversion as well as the subsequent calculation. Neither reference is universally the only correct choice; select it according to whether the question concerns original-input accuracy or stability after storage. Record reference construction with every error report. Comparing unlabeled relative errors from the two protocols would hide a changed premise, even if both display similar short numeric values.

Solve domain and lost-input exercises

At x=0, the original expression divides by zero while the reformulation returns 0.5. They cannot be called identical API behavior without specifying a domain restriction or continuous extension. Try an additional input such as 1e-8 against the same reference protocol, retaining input, precision and errors separately. One improved case does not prove a global stability property.

Would converting the already rounded large float to Decimal restore the lost difference of one? No. Decimal.from_float represents the stored value precisely; it does not know the original integer's history. Constructing Decimal from original input is a different operation. At completion retain source and output; there is no server or database to remove. Compare the result with the numerical-stability guide and explain algebraic reformulation, input representation and error metric as separate design choices.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 6

Choose a learning path