rug_calc 0.1.11

A Rug-powered scientific computing engine featuring high-performance, high-precision numerical evaluation.
Documentation

๐Ÿงฎ Rug-Calc

A zero-copy, high-performance evaluator for arbitrary-precision scientific computing. Powered by Rug, it delivers extreme accuracy and numerical stability with minimal memory overhead.

โœจ Features

  • ๐Ÿš€ High Performance: Features a non-recursive architecture that evaluates expressions in a single traversal, ensuring predictable and blazing-fast execution.
  • โš–๏ธ User-Defined Precision: Empowering users to specify custom internal bit-depths, transcending the limitations of standard hardware floats for mission-critical and research-grade calculations.
  • ๐Ÿ“ฆ Zero-Copy & Efficient: Utilizes byte-level scanning and a custom state-machine to minimize memory allocations and CPU overhead during expression parsing.
  • ๐Ÿงช Rich Mathematical Suite: Supports 40+ built-in functions, including advanced Trigonometry, Gamma, Zeta, Airy, and Error functions.
  • ๐Ÿ”ญ Scientific Constants: Instant, high-precision access to fundamental constants: Pi (P), Euler (Y), Catalan (C), and Log2 (L).
  • ๐ŸŽจ Smart Formatting: Provides versatile output options, including fixed-point rounding and "clean" string formatting with support for up to 700 decimal places.
  • ๐Ÿ›ก๏ธ Robust Stability: Designed for applications requiring extreme numerical stability and graceful error handling over panics.

๐Ÿš€ Quick Start

Add this to your Cargo.toml:

[dependencies]
rug_calc = "0.1.11"

๐Ÿ› ๏ธ Supported Syntax

rug_calc utilizes a state-machine parser to handle complex mathematical structures without recursion:

  • Basic arithmetic operations: +, -, *, /, %, ^
  • Floating-point remainder (fmod): %
  • Exponentiation (power): ^
  • Unlimited nesting depth for grouped expressions: (),
  • Scientific notation: 1.23e-5, 1.23E-5, 1.23e+5, 1.23E5
  • Constant Identifiers: P, Y, C, L
  • Operator Distinction: Automatically differentiates between as a negative sign and as a subtraction operator based on context. 0-5 vs. -5
  • Supports scientific notation: Case-insensitive e or E is supported when preceded by a digit.

๐Ÿ“š Mathematical Function Support

rug_calc provides a comprehensive suite of arbitrary-precision functions powered by the MPFR library.

  • Advanced functions: ai, abs, cos, sin, tan, csc, sec, cot, coth, ceil, cosh, sinh, tanh, sech, ln, csch, acos, asin, atan, acosh, asinh, atanh, log2, log10, sqrt, cbrt, fac, erf, li2, exp, exp2, exp10, eint, zeta, trunc, gamma, floor, frac, sgn, erfc, digamma, recip
  • Supports scientific notation: Case-insensitive e or E is supported when preceded by a digit.

๐Ÿ’Ž Constant Identifiers

To maintain parsing efficiency and avoid ambiguity with functions, constants use single-character uppercase identifiers:

  • P: Pi constant
  • Y: Euler-Mascheroni constant
  • C: Catalanโ€™s constant
  • L: Natural logarithm of 2 (Log2)

๐Ÿง  Intelligent Sign Recognition

One of the core strengths of rug_calc is its ability to distinguish between a Unary Minus (negative) and a Binary Minus (subtraction) in a single pass.

  • Smart Context Detection: The state machine automatically identifies if a is a sign prefix (e.g., -5) or an operator (e.g., 10-5) based on the preceding Marker.
  • Scientific Notation Integration: It seamlessly recognizes signs within exponents (e.g., -1e-10) without breaking the mathematical flow.

๐Ÿ’ก Example Usage

The following example demonstrates how to use.

use rug_calc::Calculator;

// Create a calculator instance with 2560-bit precision.
let mut calc = Calculator::new(2560);

// 1. Simple arithmetic with negative results
let result = calc.run("8*(6+6/2-2*5)+-2").unwrap();

// 2. Scientific Notation (Zero-Copy parsing of 'e')
// Calculates: (1.23*10^-5)*2
let sci_result = calc.run("1.23e-5*2").unwrap();
println!("Scientific result: {}", sci_result);

// 3. Advanced functions (Calculating cos(sin(ฯ€/4)))
let cos_sin_pi_4 = calc.run("cos(sin(P/4))").unwrap();

// 4. Mixing Scientific Notation with Special Constants
// Calculates: 5.0*10^12 divided by Euler's constant (Y)
let mixed_result = calc.run("5.0e+12/Y").unwrap();

// 5. Infinitely nested scientific computing
let expr = "8*6-(cos(6-3*(6/P^2-6)*3)+5)/Y*8";
let complex = calc.run(expr).unwrap();

// 6. High-precision string output (50 decimal places)
let pi_str = calc.run_round("P", Some(50)).unwrap();
println!("Pi to 50 places: {}", pi_str);

// 7. Small number formatting from scientific notation
let small_val = calc.run_round("1E-10", Some(12)).unwrap();
println!("Small value: {}", small_val); // "0.0000000001"

Contributing

Contributions are welcome! Please open issues or pull requests on GitHub.

License

This project is licensed under the MIT License.