Skip to content

Bug in division error propagation formula in uint.__div and ufloat.__div #1

Description

@gywn

There is a bug in the error propagation formula for division operations in both uint and ufloat classes. The uncertainty is significantly overestimated when dividing two uncertain numbers.

Affected Code:

  • src/uncertainty/unumbers.py, line ~122 in uint.__div
  • src/uncertainty/unumbers.py, line ~417 in ufloat.__div

Bug Description:

The current code uses:

c: float = (self._u**2) / abs(r.value)  # WRONG

But it should be:

c: float = (self._u**2) / (r.value**2)  # CORRECT

Root Cause:

The correct variance propagation formula for division A/B is:

  • Var(A/B) ≈ (σ_A/μ_B)² + (μ_A × σ_B / μ_B²)²

The bug is in the first term - the denominator is missing a square.

Reproduction:

from uncertainty.utypes import ufloat
import math

a = ufloat(10, 1)    # 10 ± 1
b = ufloat(5, 0.5)   # 5 ± 0.5

result = a / b

print(result)  # Currently: 2.0 ± 0.4899 (WRONG)
# Expected:     2.0 ± 0.2828
# Formula: σ_z ≈ |z| * sqrt((σ_x/x)² + (σ_y/y)²)

Expected Behavior:

The uncertainty should be calculated as:

result_value = a.value / b.value
result_uncertainty = abs(result_value) * math.sqrt(
    (a.uncertainty / a.value)**2 +
    (b.uncertainty / b.value)**2
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions