MathCore uses Rust’s standardDocumentation 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.
Result<T, MathError> type throughout its entire public API. There are no panics on bad input, no sentinel values, and no implicit fallbacks — every failure case surfaces as a typed error that you can inspect, match on, and recover from. The MathError enum is derived from thiserror, so all variants implement std::error::Error and produce human-readable messages via Display.
The MathError Enum
Variant Reference
ParseError(String)
Returned by MathCore::parse() (and any higher-level method that parses before proceeding) when the input string cannot be interpreted as a valid expression.
Common triggers:
- Mismatched parentheses:
"(x + 1" - Unexpected or trailing characters:
"2 + * 3","x @@" - Malformed number literals
UndefinedVariable(String)
Returned by the engine when it encounters an Expr::Symbol node whose name is not present in the active Context. The payload is the variable name.
Common triggers:
- Calling
calculateorevaluateon an expression with free variables - Typos in variable names passed to
evaluate_with_vars
UndefinedFunction(String)
Returned by the engine when a Function node’s name field does not match any built-in or registered custom function. The payload is the function name as written in the source string.
Common triggers:
- Calling an unimplemented function:
"foo(x)" - Typos in function names:
"sni(x)"instead of"sin(x)"
The parser itself does not validate function names. Parsing
"foo(x)" succeeds and produces a valid Expr::Function node. The error only surfaces when the engine tries to evaluate it.InvalidOperation(String)
A catch-all for operations that are structurally valid but not permitted for the given operand types or values. The payload is a descriptive message. Examples include:
| Situation | Message |
|---|---|
% applied to complex operands | "Modulo not defined for complex numbers" |
! applied to a negative or non-integer number | "Factorial requires non-negative integer" |
min/max called with symbolic arguments | "min requires numeric arguments" |
calculate on an expression that returns a non-real result | "Result is not a real number" |
DivisionByZero
Returned by the engine when the right-hand operand of a BinaryOp::Divide expression evaluates to zero (checked within f64::EPSILON). This applies to both real and complex denominators.
InvalidArgumentCount(String, usize, usize)
Returned when a built-in function receives the wrong number of arguments. The three fields are:
- function name — the name as it appeared in the source
- expected — the number of arguments the function requires
- got — the number of arguments actually passed
SolverError(String)
Returned by MathCore::solve() when the equation cannot be solved. The payload explains why — for example, the polynomial degree might be unsupported, or the expression may not be in a solvable form.
A
SolverError is not the same as “no real roots exist”. When the discriminant is negative for a quadratic, the solver may return complex Expr::Complex roots rather than an error, depending on the implementation path taken.Overflow
Returned by the engine when a binary operation on two Expr::Number values produces a non-finite result (i.e. f64::is_finite() returns false). This catches both f64::INFINITY and f64::NAN.
Pattern Matching Examples
Matching a Specific Variant
Handling Solver Results
Matching Parse Errors
Using the ? Operator
Because MathError implements std::error::Error, you can propagate it through your own Result-returning functions using the ? operator.
Common Error Patterns and How to Avoid Them
| Error | Common cause | Prevention |
|---|---|---|
ParseError | Unclosed brackets, operator typos | Validate user input; test expressions in unit tests |
UndefinedVariable | Calling calculate on a symbolic expression | Use evaluate for symbolic results, or supply variables via evaluate_with_vars |
UndefinedFunction | Mistyped function name | Ensure function names match the built-in list exactly |
InvalidArgumentCount | Wrong arity for log, min, sin, etc. | Consult the arity table in the parsing guide |
DivisionByZero | Dividing by a sub-expression that evaluates to zero | Check denominators before evaluation, or handle the error gracefully |
Overflow | Very large exponents or products | Clamp inputs or use evaluate and check for Expr::Complex or symbolic fallback |
SolverError | Unsupported equation form or degree | Check degree() before calling solve; handle non-polynomial equations separately |
