Skip to content

complex: Fix ln1p precision loss for small arguments#1635

Open
duncanita wants to merge 1 commit intoc3d:devfrom
duncanita:fix/complex-ln1p-precision
Open

complex: Fix ln1p precision loss for small arguments#1635
duncanita wants to merge 1 commit intoc3d:devfrom
duncanita:fix/complex-ln1p-precision

Conversation

@duncanita
Copy link

Summary

  • Fix catastrophic cancellation in complex ln1p for small arguments by using the identity ln(1+z) = 2*atanh(z/(z+2)) instead of ln(|1+z|) + i*arg(1+z)
  • Wrap with SaveSetAngleUnits to ensure atan2 uses radians internally regardless of calculator angle mode
  • Add 9 tests covering zero, real axis, purely imaginary, small values, negative real, high precision, and round-trip ln1p/expm1 identity
  • Restore precision settings after identity test to avoid affecting subsequent tests

Test plan

  • Full test suite passes (3315 tests, 0 failures)
  • Benchmarked 1000 iterations of complex ln1p: ~0.6ms per call, confirming integer::make() allocation overhead is negligible (<0.3%)

Use the identity ln(1+z) = 2*atanh(z/(z+2)) which avoids catastrophic
cancellation when |z| is small. The previous formula ln(1+z) = ln(|1+z|) + i*arg(1+z)
lost nearly all significant digits for small z because 1+z ≈ 1.

Also wrap with SaveSetAngleUnits to ensure atan2 uses radians internally,
preventing wrong results when the calculator is in degrees mode.

Add 9 tests covering zero, real axis, purely imaginary, small values,
negative real, high precision, and round-trip ln1p/expm1 identity.
Restore precision settings after the identity test to avoid affecting
subsequent tests.
@duncanita duncanita changed the base branch from stable to dev February 18, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments