TheDocumentation 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.
mathcore::precision module provides lossless numerical computation through the PrecisionNumber enum, which unifies exact rational arithmetic (BigRational), arbitrary-precision integers (BigInt), and ordinary floating-point values (f64) under a single type. The companion ArbitraryPrecision struct computes mathematical constants such as π and e to user-specified precision using convergent rational series.
The backing types BigInt and BigRational come from the num-bigint and num-rational crates respectively. Mixed-type arithmetic is handled by promoting both operands to BigRational before operating.
pub enum PrecisionNumber
The central type of the module. Holds a number in one of three precision representations.
| Variant | Backing type | Use case |
|---|---|---|
Exact(BigRational) | num_rational::BigRational | Rational arithmetic without rounding, e.g. 1/3 + 1/6 = 1/2 exactly. |
Float(f64) | f64 | Approximate values; used as a fallback when exact representation is unavailable. |
Integer(BigInt) | num_bigint::BigInt | Whole numbers of any size, e.g. factorials, cryptographic primes. |
from_f64
Constructs a PrecisionNumber from an f64. If the value is finite and has no fractional part, it is stored as Integer(BigInt). Otherwise it is stored as Float(f64).
The floating-point value to convert. Integral values such as
4.0 become Integer(4); fractional values such as 3.14 become Float(3.14).from_str_with_precision
Parses a string into a PrecisionNumber, preserving exactness where possible. The parsing rules are applied in order:
- Integer — if the string is a valid integer literal (e.g.
"42","-7"), returnsInteger(BigInt). - Rational — if the string contains a
/(e.g."3/4","-1/7"), parses numerator and denominator and returnsExact(BigRational). - Decimal — if the string contains a
.(e.g."2.5","0.333"), converts the decimal to an exact rational (e.g.25/10), returningExact(BigRational). - Float fallback — attempts
str.parse::<f64>()and returnsFloat.
A string representation of the number. Supported formats: integer literals,
"p/q" fraction notation, and decimal notation.Ok(PrecisionNumber) on success, or Err(MathError::ParseError) when the string cannot be parsed in any supported format.
to_rational
Converts the PrecisionNumber to a BigRational. For Exact and Integer variants this is lossless. For Float, the conversion uses BigRational::from_float, falling back to BigRational::zero() for non-finite values.
add
Adds two PrecisionNumber values, preserving the highest level of precision available:
Integer + Integer→IntegerExact + Exact→ExactFloat + Float→Float- Mixed types → both promoted to
BigRational, result isExact
The value to add to
self.subtract
Subtracts other from self using the same precision-preservation rules as add.
The value to subtract from
self.multiply
Multiplies two PrecisionNumber values with the same precision-preservation rules as add.
The multiplier.
divide
Divides self by other. Returns Err(MathError::DivisionByZero) when other is zero.
The divisor. Must not be zero.
Ok(PrecisionNumber) on success, or Err(MathError::DivisionByZero) when the divisor is zero.
factorial
Computes the factorial of a non-negative Integer value using exact BigInt arithmetic. Produces arbitrarily large results without overflow.
Ok(PrecisionNumber::Integer(n!)) for non-negative integer inputs, or Err(MathError::InvalidOperation) for negative values or non-integer variants.
is_zero
Returns true if the value represents zero. For Float, uses f64::EPSILON as the threshold.
to_f64
Converts the PrecisionNumber to an Option<f64>. Returns None if the value cannot be represented as f64 (e.g. a BigInt too large for f64).
Some(f64) for values that can be approximated, or None when the conversion is not possible.
power
Raises self to the power given by exponent. Integer exponentiation is computed exactly using BigInt::pow. Negative integer exponents produce an Exact(BigRational) result. All other combinations fall back to f64::powf and return Float.
The exponent. Negative
Integer exponents are supported and produce an exact rational result.Ok(PrecisionNumber) with the result, or Err(MathError::InvalidOperation) when the exponent is too large to handle exactly as a u32.
sqrt
Computes the square root of self. For non-negative Integer values the result is Integer when the square root is exact and Float otherwise. Float inputs use f64::sqrt. Exact(BigRational) inputs are converted to f64 before taking the root. Negative values return Err.
Ok(PrecisionNumber) containing the square root, or Err(MathError::InvalidOperation) for negative inputs, or Err(MathError::Overflow) when the integer value cannot be converted to f64.
pub struct ArbitraryPrecision
Computes mathematical constants to arbitrary precision using rational series. All results are returned as PrecisionNumber::Exact(BigRational).
compute_pi
Approximates π using the Bailey-Borwein-Plouffe (BBP) formula:
PrecisionNumber::Exact(BigRational) whose accuracy improves with higher precision.
Number of BBP series terms to sum. Each additional term roughly adds ~1.2 decimal digits of accuracy. A value of
50 gives roughly 60 correct decimal digits.PrecisionNumber::Exact(BigRational) approximating π.
compute_e
Approximates Euler’s number e using the standard factorial series:
PrecisionNumber::Exact(BigRational) that converges rapidly — 20 terms yield about 18 correct decimal digits.
Number of terms to sum in the series
1 + 1/1! + 1/2! + .... Convergence is fast; precision = 30 gives more than 30 correct digits.PrecisionNumber::Exact(BigRational) approximating e.
compute_sqrt
Iteratively approximates the integer square root of a non-negative BigInt using the Babylonian (Heron’s) method. The iteration x ← (x + n/x) / 2 is repeated precision times. When the result squared equals n exactly, a PrecisionNumber::Integer is returned; otherwise the final BigInt approximation is returned as Integer. Negative inputs immediately return PrecisionNumber::Float(f64::NAN).
The non-negative integer whose square root is to be approximated. Negative values produce
PrecisionNumber::Float(NAN).Number of Babylonian iterations to perform. More iterations converge closer to the true integer square root.
PrecisionNumber::Integer containing the approximate (or exact) integer square root, or PrecisionNumber::Float(f64::NAN) for negative input.
