Skip to main content

Documentation 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.

The Limits module in MathCore lets you evaluate the limiting behaviour of an expression as a variable approaches a finite value or infinity. It supports two-sided limits, one-sided limits (from the left or the right), and limits at ±∞, and exposes a separate continuity checker that combines limit evaluation with direct function evaluation. For the common 0/0 indeterminate form, Limits also provides automatic L’Hôpital’s rule application.

Import

use mathcore::calculus::limits::{Limits, LimitDirection};
You will also need MathCore::parse to turn a string into an Expr before passing it to Limits:
use mathcore::MathCore;
use mathcore::calculus::limits::{Limits, LimitDirection};

LimitDirection

Controls which side of the approach point is used for evaluation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LimitDirection {
    Left,   // approach from below  (x → point⁻)
    Right,  // approach from above  (x → point⁺)
    Both,   // two-sided; errors if Left ≠ Right
}
VariantMeaning
LeftApproach point from the left (x → point⁻).
RightApproach point from the right (x → point⁺).
BothChecks that the left and right limits agree; returns that value.
When LimitDirection::Both is used and the one-sided limits differ, Limits::limit returns Err(MathError::InvalidOperation("Limit does not exist (left != right)")).

Limits::limit

Computes the limit of an expression as a variable approaches a finite point.
pub fn limit(
    expr: &Expr,
    var: &str,
    point: f64,
    direction: LimitDirection,
) -> Result<Expr, MathError>

Parameters

ParameterTypeDescription
expr&ExprThe expression whose limit is being computed.
var&strThe variable that is approaching point.
pointf64The value the variable approaches.
directionLimitDirectionWhich side to approach from (Left, Right, or Both).

Return value

Returns Ok(Expr::Number(value)) for a finite limit, Ok(Expr::Symbol("∞")) or Ok(Expr::Symbol("-∞")) for an infinite limit, or Err(MathError) if the limit does not exist or cannot be computed.

Examples

Classic limit: lim(x→0) sin(x)/x = 1

use mathcore::MathCore;
use mathcore::calculus::limits::{Limits, LimitDirection};

let expr = MathCore::parse("sin(x)/x").unwrap();
let limit = Limits::limit(&expr, "x", 0.0, LimitDirection::Both).unwrap();

println!("lim(x→0) sin(x)/x = {}", limit); // 1
sin(x)/x is undefined at x = 0, but its two-sided limit is 1. MathCore evaluates the expression numerically at points approaching the limit from both sides, so it handles this removable discontinuity correctly.

One-sided limits

One-sided limits are useful for expressions that are discontinuous or undefined at a point:
use mathcore::MathCore;
use mathcore::calculus::limits::{Limits, LimitDirection};

// lim(x→0⁻) 1/x = -∞
let expr = MathCore::parse("1/x").unwrap();
let left = Limits::limit(&expr, "x", 0.0, LimitDirection::Left).unwrap();
println!("lim(x→0⁻) 1/x = {}", left); // -∞
Because the left and right limits disagree, requesting LimitDirection::Both for 1/x at 0.0 returns an error — the two-sided limit does not exist.

Finite point limit

use mathcore::MathCore;
use mathcore::calculus::limits::{Limits, LimitDirection};

// lim(x→2) x^2 = 4
let expr = MathCore::parse("x^2").unwrap();
let limit = Limits::limit(&expr, "x", 2.0, LimitDirection::Both).unwrap();

if let mathcore::Expr::Number(n) = limit {
    assert!((n - 4.0).abs() < 1e-9);
    println!("lim(x→2) x^2 = {}", n); // 4
}

Limits::limit_at_infinity

Evaluates the limit of an expression as the variable tends to positive or negative infinity.
pub fn limit_at_infinity(
    expr: &Expr,
    var: &str,
    positive: bool,
) -> Result<Expr, MathError>

Parameters

ParameterTypeDescription
expr&ExprThe expression to evaluate.
var&strThe variable tending to ±∞.
positivebooltrue for +∞, false for −∞.

Examples

use mathcore::MathCore;
use mathcore::calculus::limits::Limits;

let expr = MathCore::parse("1/x").unwrap();
let limit = Limits::limit_at_infinity(&expr, "x", true).unwrap();

if let mathcore::Expr::Number(n) = limit {
    println!("lim(x→+∞) 1/x = {:.6}", n); // ≈ 0.000000
}

Limits::is_continuous_at

Checks whether an expression is continuous at a point by comparing the function’s value there with its two-sided limit.
pub fn is_continuous_at(
    expr: &Expr,
    var: &str,
    point: f64,
) -> Result<bool, MathError>

Parameters

ParameterTypeDescription
expr&ExprThe expression to test.
var&strThe variable at whose value continuity is checked.
pointf64The point at which to check continuity.

Return value

Returns Ok(true) if the two-sided limit exists and equals the function’s value at point (within a tolerance of 1e-9). Returns Ok(false) if the function is undefined at the point, the limit does not exist, or the limit and value differ. Returns Err(MathError) only for expression evaluation failures.

Example

use mathcore::MathCore;
use mathcore::calculus::limits::{Limits, LimitDirection};

// sin(x)/x is not defined at 0 but its limit is 1 — not continuous
let expr = MathCore::parse("sin(x)/x").unwrap();
let continuous = Limits::is_continuous_at(&expr, "x", 1.0).unwrap();
println!("sin(x)/x continuous at x=1: {}", continuous); // true

let at_zero = Limits::is_continuous_at(&expr, "x", 0.0).unwrap();
println!("sin(x)/x continuous at x=0: {}", at_zero); // false

L’Hôpital’s rule

Limits::lhopital_rule handles 0/0 indeterminate forms by automatically differentiating numerator and denominator and re-evaluating the limit:
pub fn lhopital_rule(
    numerator: &Expr,
    denominator: &Expr,
    var: &str,
    point: f64,
) -> Result<Expr, MathError>
If both the numerator and denominator evaluate to zero at point, MathCore differentiates both with Calculus::differentiate and recomputes the limit of the resulting ratio. Otherwise it falls back to a direct limit evaluation.
use mathcore::MathCore;
use mathcore::calculus::limits::Limits;

// lim(x→0) sin(x)/x via L'Hôpital: cos(x)/1 → 1
let num = MathCore::parse("sin(x)").unwrap();
let den = MathCore::parse("x").unwrap();

let limit = Limits::lhopital_rule(&num, &den, "x", 0.0).unwrap();
println!("L'Hôpital result: {}", limit); // 1
You can call lhopital_rule proactively for any expression you suspect is a 0/0 form — if it is not, the function falls back gracefully to a standard numerical limit evaluation.

Common limit patterns

ExpressionPointDirectionResult
sin(x)/x0.0Both1
1/x0.0Right
1/x0.0Left-∞
1/x+∞0
exp(x)+∞
exp(-x)+∞0
x^2any aBoth
(x^2 - 1)/(x - 1)1.0Both2
The Limits module uses a numerical approach — evaluating the expression at points very close to the limit point (offset by ±1e-10) and checking for convergence. This means results are subject to floating-point precision. For limits that require symbolic cancellation of terms (such as (x^2 - 1)/(x - 1) at x = 1), use lhopital_rule or simplify the expression with MathCore::simplify before calling Limits::limit.

Build docs developers (and LLMs) love