Skip to content

feat(float): implement arbitrary-precision trigonometric functions and constants#60

Open
CokieMiner wants to merge 2 commits into
cmpute:masterfrom
CokieMiner:feat/trigonometry
Open

feat(float): implement arbitrary-precision trigonometric functions and constants#60
CokieMiner wants to merge 2 commits into
cmpute:masterfrom
CokieMiner:feat/trigonometry

Conversation

@CokieMiner
Copy link
Copy Markdown

Overview

This PR introduces a comprehensive suite of trigonometric primitives and constants to the dashu-float crate. The implementation follows the library's architectural roadmap by consolidating advanced functions into a new math module and adopting a non-panicking FpResult pattern for robust error handling.

Key Features

  • Complete Trigonometric Suite: Implements sin, cos, tan, asin, acos, atan, atan2, and an optimized joint sin_cos evaluation.
  • *High-Performance $\pi: Implements the Chudnovsky algorithm with Binary Splitting, providing state-of-the-art performance for high-precision constant generation.
  • Non-Panicking API: All functions return FpResult<B> (wrapping Normal, NaN, Infinite, etc.), ensuring safety for domain errors and singularities without library crashes.
  • Mathematically Rigorous:
    • IEEE 754 Compliance: Full support for signed infinities and edge cases in atan2.
    • Numerical Stability: Magnitude-aware range reduction and dynamic guard-digit calculation ( + \lceil\log(x)\rceil + 50$) to prevent catastrophic cancellation.
    • Base-Agnostic Precision: Dynamic bit-mapping using EstimatedLog2 ensures correctness across all bases (binary, decimal, etc.).

Internal Improvements

  • Robust Rounding: Patched a bug in split_at_point_internal where intermediate results with high digit counts could trigger assertion failures for values $|x| &lt; 1.0$.
  • API Ergonomics: Exposed functions via both the Context API and inherent methods on FBig, with clear documentation on panic conditions and return types.

Verification Results

  • Fuzz Testing: Validated against the MPFR library with 1,500+ randomized iterations covering extreme precision ranges (up to 1,000 digits) and exponents (up to $\pm 500$).
  • Unit Tests: Comprehensive coverage in tests/trig.rs for all primitives and IEEE 754 infinity logic.
  • Standards: Verified clean by cargo clippy and maintained no_std compatibility by using pure integer and core-compliant arithmetic.

Performance Note

The Chudnovsky implementation leverages dashu's fast multiplication for (M(n)\log^2 n)$ complexity. A // TODO has been added for future static caching of $\pi$ to further optimize repeated calls at common precisions.

@cmpute
Copy link
Copy Markdown
Owner

cmpute commented Apr 23, 2026

Thanks for the PR! I will try to find a time to review and merge 👍

@CokieMiner
Copy link
Copy Markdown
Author

I tried to follow more or less what was there already if you don't agree with any design decision just ask for the changes

@CokieMiner
Copy link
Copy Markdown
Author

CokieMiner commented Apr 23, 2026

Just a note I forgot to leave there are still some panick paths that I left that in principle should never happen but if you want to discuss how to handle them I will patche to crate design preferences

- Fix catastrophic precision loss in argument reduction for large inputs
  (x > 10^500) by scaling guard digits with magnitude.
- Optimize memory by implementing a zero-clone range reduction logic
  and avoiding UBig clones in Chudnovsky's algorithm.
- Fix 1-ulp errors in tan() by computing sin/cos division within
  the high-precision work context.
- Refactor atan2() to handle signed infinities correctly and
  standardize FpResult error reporting.
- Hardened test suite with aggressive Rug-based fuzzing (2000 iterations)
  and regression cases for massive exponents.
@CokieMiner
Copy link
Copy Markdown
Author

Update: Enhanced Precision Stability & Performance Optimizations

I've just pushed a series of updates to address some edge-case precision issues and optimize memory usage:

  • Precision Guarding: Refined the guard digit calculation in compute_work_context to scale with input magnitude ($50 + \text{mag}/10$). This ensures $100%$ accuracy during argument reduction even for massive exponents (e.g., $10^{2000}$), which previously suffered from catastrophic cancellation.
  • Tangent Accuracy: Fixed a 1-ulp precision leak in tan() by computed the intermediate sin/cos division within the high-precision work context before the final rounding.
  • Zero-Clone Optimization: Refactored the range reduction logic to be "zero-clone" by reordering operations to consume owned values only after their final use as references.
  • Memory Efficiency: Optimized the Chudnovsky algorithm base case in consts.rs to use reference-based multiplication, significantly reducing allocations during $\pi$ constant generation.
  • Aggressive Fuzzing: Hardened the test suite with a 2000-iteration random fuzzing pass against rug, covering extreme exponent ranges and Pythagorean identity checks.

The implementation is now significantly leaner and more robust against extreme scale inputs.

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.

2 participants