checked design
This page explains which operations on dual numbers have checked forms, how
their errors arise from the derivative rules, and why the facade reuses the
arithmetic error model.
Design goal
Let differentiated code report invalid operations as values, with the same error type that scalar Luna Flow code uses, and never return a valid value with an invalid derivative. The section on floating-point domains shows where the current implementation falls short of the second goal.
Constraints
- The checked traits and errors belong to
Luna-Flow/arithmetic; this repository can implement them forDual[T]but cannot add variants. - A checked dual operation can only use the checked operations of
T, so its errors are those ofT’s instances. Dual[T]keeps the unchecked operators/andsqrtfor ordinary formulas; the checked forms are separate methods.
Mathematical background
Where a derivative fails
A checked operation must fail when either component of is undefined. For the two checked operations:
The quotient fails exactly where the value fails, because in exact arithmetic. The square root is different: exists at , but is not differentiable there (its difference quotient is unbounded), so the domain of the dual operation is the open half-line , smaller than the domain of the scalar square root.
Floating-point domains
In Double, can be zero for non-zero . With rounding to nearest
and gradual underflow, rounds to exactly when ,
half the smallest subnormal number, that is for
, even though may be
finite. The checked quotient then fails in the tangent: with a division by
zero when , and with the domain error of when
, for example for a constant divided by a constant, whose true
tangent is .
At the other end overflows to for
. T’s div_checked accepts a
finite numerator over , so the checked quotient returns Ok with
tangent even when the true tangent is a normal number.
This breaks the design goal: a checked operation returns a wrong derivative
without an error. Rescale such divisors; the
dual design derives the ranges and a formula that
avoids squaring .
Design decisions
Check both components
Problem. A checked scalar operation can succeed while the derivative does not exist.
Choice. Each checked dual operation calls the checked scalar operation
for the value and div_checked for the tangent division, and returns the
first error. This makes the dual domain the intersection of the domains of
and , as derived above; in particular sqrt_checked fails at .
No special case for a zero tangent
At with the tangent is the indeterminate . One could
return (a constant input has no derivative to report), but that would
make the result depend on how a constant was produced. The implementation
lets T decide: for Double this is a domain error.
Reuse arithmetic
Dual[T] implements DivChecked and SqrtChecked from arithmetic, and
this facade re-exports exactly those traits with ArithmeticContext,
ArithmeticError, ArithmeticErrorKind and RoundingMode. Generic checked
code therefore runs on Double and on Dual[Double] without adaptation,
and errors from both levels have one type.
Only division and square root
arithmetic defines checked traits for division, square root, comparison,
parsing and integer powers. Of these, only division and square root are
operations on with a derivative rule in this repository, so
only they have checked dual forms. Logarithms and trigonometric functions
follow the unchecked semantics of T.
Correctness and invariants
div_checked(x, y)succeeds exactly whenT’sdiv_checkedsucceeds for both and ; then it equalsx / ycomputed with the same operations.sqrt_checked(x)succeeds exactly whenT’ssqrt_checked(a)anddiv_checked(b, 2√a)succeed; forDoublethat is , or NaN.- The context is passed to
Tunchanged and never modified.
Alternatives rejected
- A dedicated autodiff error type. It would duplicate
ArithmeticErrorand force conversions at every boundary. Optionresults. They lose the reason for the failure.- Checking only the value. It would return infinite or NaN derivatives from a “checked” operation.
Boundaries
- No checked logarithm, exponential or trigonometric operations.
- No contextual (
ArithmeticOutcome) operations and no rounding diagnostics. - No automatic rescaling of tiny divisors.