Expand description
§Rug-Calc
rug_calc is a high-performance, arbitrary-precision expression evaluator.
It bridges the gap between raw MPFR calculations and user-friendly string expressions.
§Why Rug-Calc?
- High Performance: Non-recursive implementation, calculation completed in a single traversal.
- 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.
- Comprehensive Function Support: Supports over 40 mathematical functions, including trigonometry, hyperbolic functions, Gamma, Zeta, and Airy functions.
- Zero-Copy Logic: Efficiently parses expressions using byte-level scanning and state-machine logic to minimize overhead. ensuring safe integration into production systems.
§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-5vs.-5 - Supports scientific notation: Case-insensitive
eorEis 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
eorEis 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 constantY: Euler-Mascheroni constantC: Catalan’s constantL: 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 complex = calc.run("8*6-(cos(6-3*(6/P^2-6)*3)+5)/Y*8").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"Structs§
- Calculator
- The core Calculation engine.
Enums§
- Calc
Error - Errors that can occur during expression parsing or mathematical evaluation.