Floating-point arithmetic accumulates rounding errors that are invisible until they cause real problems — a financial calculation that is off by a cent, a symbolic result that should be exactlyDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
1/2 but prints as 0.49999999999. MathCore’s precision module solves this by providing PrecisionNumber, a three-variant enum that represents numbers as exact integers (BigInt), exact rationals (BigRational), or floating-point values — and the ArbitraryPrecision struct for computing transcendental constants to as many digits as you need.
Import
PrecisionNumber
Exact(BigRational) so no precision is lost.
Construction
PrecisionNumber::from_str_with_precision(s: &str) -> Result<PrecisionNumber, MathError>
Parses a string into the most precise representation available:
| Input format | Result variant | Example |
|---|---|---|
| Plain integer | Integer(BigInt) | "42" → Integer(42) |
Rational n/d | Exact(BigRational) | "1/3" → Exact(1/3) |
| Decimal | Exact(BigRational) | "0.25" → Exact(1/4) |
| Other float | Float(f64) | "3.14" → Float(3.14) |
Decimal strings like
"2.5" are stored as BigRational (5/2), not f64. This means subsequent arithmetic on them remains exact.PrecisionNumber::from_f64(value: f64) -> PrecisionNumber
Converts an f64 to a PrecisionNumber. If the value has no fractional part and is finite, it is stored as Integer; otherwise as Float.
Arithmetic Methods
All arithmetic methods return a newPrecisionNumber; the originals are not mutated. Mixed-variant operations are promoted to Exact automatically.
add(&other) -> PrecisionNumber
subtract(&other) -> PrecisionNumber
multiply(&other) -> PrecisionNumber
divide(&other) -> Result<PrecisionNumber, MathError>
Division by zero returns Err(MathError::DivisionByZero).
power(&exponent) -> Result<PrecisionNumber, MathError>
Integer ^ Integer(non-negative exponent): returnsIntegerInteger ^ negative Integer: returnsExact(1/base^|exp|)- All others: promotes to
Float
factorial() -> Result<PrecisionNumber, MathError>
Computes exact factorial for non-negative integers. Returns Err for negative or non-integer inputs. Handles arbitrarily large values — 100! is computed exactly.
sqrt() -> Result<PrecisionNumber, MathError>
Returns Integer if the result is a perfect square, otherwise Float.
Conversion Methods
to_rational() -> BigRational
Converts any variant to BigRational. For Float, uses BigRational::from_float (may lose precision for irrational floats).
to_f64() -> Option<f64>
Returns the value as an f64, or None if the number is too large to represent.
is_zero() -> bool
Returns true for integer zero, rational zero, or a Float within f64::EPSILON of zero.
ArbitraryPrecision
ArbitraryPrecision is a unit struct that provides static methods for computing well-known constants using rational series.
ArbitraryPrecision::compute_pi(precision: usize) -> PrecisionNumber
Computes π using the Bailey-Borwein-Plouffe (BBP) formula, summing precision terms of the series:
BigRational, so the result is an Exact rational approximation. More terms produce a more accurate rational.
The
precision argument is the number of BBP series terms, not the number of decimal digits. Roughly 15–20 terms are sufficient to exceed f64 precision. Use larger values (e.g. 100+) for applications requiring many significant figures.ArbitraryPrecision::compute_e(precision: usize) -> PrecisionNumber
Computes Euler’s number e via the Taylor series Σ_^ 1/n!:
ArbitraryPrecision::compute_sqrt(n: &BigInt, precision: usize) -> PrecisionNumber
Computes the integer square root of n using Newton’s method with precision iterations. Returns Integer for perfect squares, otherwise the closest integer approximation.
Full Example
Use Cases
Financial calculations — avoid the classic0.1 + 0.2 ≠ 0.3 trap:
