Every fallible operation in MathCore returnsDocumentation 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>. Whether you are parsing an expression string, evaluating with runtime variables, differentiating symbolically, or solving an equation, MathError is the single error type you will encounter. It is derived with thiserror, which means it automatically implements both std::error::Error and Display using the #[error("…")] message defined on each variant. This makes MathError composable with any error-handling strategy — direct match, the ? operator, anyhow, eyre, or your own custom error type.
pub enum MathError
Variants
ParseError(String)
Display: "Parse error: {message}"
Returned when the input expression string cannot be turned into a valid Expr tree. The inner String contains a human-readable description of the parse failure — for example, unexpected tokens, mismatched parentheses, or an empty input.
Common causes
- Mismatched or missing parentheses:
"sin(x","(2 + 3" - Unknown or malformed token sequences:
"2 ** 3","x ++ 1" - Empty string input
UndefinedVariable(String)
Display: "Undefined variable: {name}"
Returned during evaluation when the expression contains a Symbol that is not bound in the active context or variable map. The inner String is the variable name.
Common causes
- Forgetting to supply a variable in
evaluate_with_vars - Typo in a variable name (
"X"vs"x")
UndefinedFunction(String)
Display: "Undefined function: {name}"
Returned when the expression calls a function name that the engine does not recognise and that has not been registered as a custom function in the Context. The inner String is the function name as it appeared in the expression.
Common causes
- Misspelling a built-in function name:
"sinn(x)","Cos(x)" - Using a function name before registering it in a
Context
InvalidOperation(String)
Display: "Invalid operation: {message}"
Returned when an operation is structurally valid but cannot be applied to the given expression type. The inner String provides context. For example, calculate returns this when evaluation produces a non-numeric Expr (such as a complex or symbolic result), and plot_ascii returns this when no finite points can be computed.
Common causes
- Calling
calculateon an expression that evaluates to aComplexresult - Calling
plot_asciion a range where every sampled point is non-finite - Applying a numeric operation to a still-symbolic expression
DivisionByZero
Display: "Division by zero"
Returned when the engine detects a division by zero at evaluation time. This variant carries no inner data — the error message is always the same.
InvalidArgumentCount(String, usize, usize)
Display: "Invalid argument count for function {name}: expected {expected}, got {got}"
Returned when a function call in the expression supplies the wrong number of arguments. The three fields are the function name, the expected arity, and the actual arity.
Common causes
"sin(x, y)"—sinexpects exactly 1 argument"atan2(x)"—atan2expects exactly 2 arguments
SolverError(String)
Display: "Cannot solve equation: {message}"
Returned by the solver subsystem when the equation has no solution, has infinitely many solutions, or uses a form the solver does not support. The inner String explains the specific failure.
Common causes
- No real roots exist:
"x^2 + 1"has no real solutions - Transcendental equations the symbolic solver cannot handle:
"sin(x) - x" - Expressions that are not polynomial in the specified variable
Overflow
Display: "Numeric overflow"
Returned when a calculation produces a value too large to represent as an f64 (i.e. the result is f64::INFINITY or f64::NEG_INFINITY due to overflow, rather than a mathematically infinite value).
Common causes
- Factorial of a very large number
- Exponentiation with extremely large exponents
Pattern matching
MathError is an ordinary Rust enum, so you can match exhaustively or selectively. The most common pattern is a match over the Result returned by any MathCore method:
Using the ? operator
Because MathError implements std::error::Error, it works naturally with the ? operator inside functions that return Result<_, MathError> or any error type that MathError converts into:
Display and std::error::Error
All variants produce a consistent error message through Display, thanks to thiserror. You can log, format, or propagate them like any other Rust error:
