In this article

A reconciliation assertion can fail even when printed amounts look equal because binary floating-point stores approximations of many decimal fractions. Reproduce the exact failing expression, inspect how values enter the calculation, and choose a representation and rounding policy that match the domain. This worked Python example uses Decimal strings; it does not prescribe one rounding rule for every financial system.

Reproduce the reconciliation failure

Suppose a test splits a one-dollar total into three ten-cent entries. The interface displays values with two digits, but an exact assertion fails. Start with the smallest expression that reproduces the mismatch rather than asking an assistant to rewrite the whole accounting function. Record the actual and expected values at full precision.

A compact failing check is three additions of 0.1 compared with 0.3. The display may hide the stored approximation, while the Boolean comparison exposes it. Python's floating-point tutorial documents this equality surprise and explains that most decimal fractions cannot be represented exactly as binary fractions. Floating-Point Arithmetic: Issues and Limitations

Failing exact-equality check
Codepython
parts = [0.10, 0.10, 0.10]
actual = sum(parts)
expected = 0.30
print(repr(actual), actual == expected)
# 0.30000000000000004 False

    Trace representation instead of formatting

    Formatting a value to two places changes its presentation, not the calculation that produced it. Likewise, rounding each intermediate value can move the result according to a rule you have not specified. The debugging question is whether the domain expects approximate measurement or exact decimal accounting, and when any required rounding occurs.

    Binary floating point is useful for many scientific and engineering calculations; its behavior is not evidence of a Python defect. But if the invariant is that entered decimal cents reconcile exactly, comparing binary approximations for exact equality is the wrong representation. Python's tutorial distinguishes display rounding from the stored value. Floating-Point Arithmetic: Issues and Limitations

    • Capture repr(actual) or an equivalent full-precision diagnostic before adding formatting.

    • Check whether inputs came from decimal text, an integer unit, or an earlier float conversion.

    Use Decimal from the original text

    Python's Decimal type can represent decimal strings exactly and is designed to support fixed-point as well as rounded decimal arithmetic. Construct Decimal from the original text such as 0.10. Converting an already-created float to Decimal preserves that float approximation; it cannot recover the decimal intent that was lost at input. decimal — Decimal fixed-point and floating-point arithmetic

    The repaired example sums the same three inputs from strings. The independent expected result is also created from a string. Decimal avoids this specific binary representation mismatch, but it does not decide the business policy for taxes, fractional cents, or allocation remainders.

    Decimal total with explicit scale
    Codepython
    from decimal import Decimal, ROUND_HALF_UP
    
    parts = [Decimal("0.10"), Decimal("0.10"), Decimal("0.10")]
    actual = sum(parts, Decimal("0"))
    expected = Decimal("0.30")
    assert actual == expected
    
    # If the domain requires cents, state the chosen rounding rule.
    rounded = Decimal("1.005").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
    print(actual, rounded)  # 0.30 1.01

      Make scale and rounding policy explicit

      A Decimal calculation still has a context: precision, rounding, and signal handling affect operations. Multiplication or division can produce more fractional digits than the currency supports. Decide whether to round per line, per tax component, or only at settlement, and select the rule required by the domain. The example's ROUND_HALF_UP is illustrative; it is not a universal accounting requirement. decimal — Decimal fixed-point and floating-point arithmetic

      Quantize makes a scale decision visible in code. It does not make an arbitrary rule correct. If the product must allocate a remainder among several recipients, specify where the extra cent goes and test that allocated rows still add to the original total. If inputs are already integer minor units, integer arithmetic may be a simpler representation.

        Test the invariant with an independent oracle

        Do not replace the equality assertion with a broad epsilon simply to make it green. A tolerance is appropriate when the domain itself is approximate and the acceptable error is specified. It can conceal a defect when cents must reconcile exactly. Test a concrete invariant from decimal input and calculate the expected value independently from the implementation's aggregation path.

        For this fixture, assert the sum is Decimal 0.30 and that the chosen quantize rule maps 1.005 to 1.01. Add cases at half-cent boundaries and negative values if the domain requires them. Python's Decimal documentation describes configurable rounding modes and signals, so tests should make the selected context visible rather than relying on a hidden default. decimal — Decimal fixed-point and floating-point arithmetic

        A useful debugging note records both the input representation and the arithmetic operation. The text 0.10 communicates two decimal places, while a float value does not preserve that written scale. Decimal also retains significance in its representation, but the business rule must still define whether trailing zeroes are presentation, validation, or accounting data. Keep these questions separate: numerical equality, output formatting, and allocation policy are related but not interchangeable tests.

        • Input values are parsed from strings, not converted from floats.

        • The total matches an independently written decimal expectation.

        • Rounding boundaries and remainder allocation are covered when the product uses them.

        Common questions about decimal debugging

        Should all floating-point values be replaced with Decimal? No. Choose a numeric representation that matches the contract; measurement and simulation often need binary floating point, while exact decimal accounting may need decimal or integer minor units. Floating-Point Arithmetic: Issues and Limitations

        Does Decimal automatically round to two places? No. It supports decimal arithmetic with a configurable context, but the application still needs an explicit scale and rounding rule. Is round() on a float enough? It may change a displayed or rounded result, but it does not make the underlying binary representation decimal-exact. Start from the original input and preserve the domain's intended precision. decimal — Decimal fixed-point and floating-point arithmetic

        When debugging with AI, provide the minimal failing expression, exact output, input representation, and required invariant. Ask for competing hypotheses, then check each one with a small independent assertion. The repair is complete only when the test captures the rule the product actually needs.

          Sources & editorial notes

          This article was authored with AI using primary-source research and an automated technical review. It has not been reviewed by a human. Code examples are illustrative; run and verify them in your own environment.

          1. Floating-Point Arithmetic: Issues and Limitations

            Explains binary approximation of decimal fractions and why exact float equality can fail despite compact display.

          2. decimal — Decimal fixed-point and floating-point arithmetic

            Documents exact decimal construction, accounting use, configurable precision, rounding modes, and quantize behavior.

          nrml promotion

          Put the reading into practice

          Work through an existing repository, review AI suggestions, and test the decisions behind your changes.

          Explore practice problems