Skip to content
Open in Anthropic

Determinism and Numeric Behavior

This document specifies TeXpr's guarantees about evaluation determinism, floating-point behavior, simplification rules, and cross-platform consistency. Understanding these properties is critical for applications requiring reproducible results.

Floating-Point Arithmetic

IEEE 754 Compliance

All numeric operations use Dart's double type, which implements IEEE 754 binary64 (64-bit double-precision floating-point).

PropertyValue
Precision~15-17 significant decimal digits
Range±1.8 × 10³⁰⁸
Smallest positive5 × 10⁻³²⁴ (subnormal)
Epsilon2.22 × 10⁻¹⁶

Special Values

TeXpr propagates IEEE 754 special values:

ValueWhen ProducedExample
InfinityOverflow, division by zero1e308 * 10, 1/0
-InfinityNegative overflow-1e308 * 10
NaNIndeterminate forms0/0, sqrt(-1) for real-only

WARNING

NaN and Infinity are valid results, not exceptions. Always check with isNaN and isInfinite in your application code.

Precision Limitations

dart
// Associativity is not guaranteed
(1e15 + 1.0) - 1e15  // May not equal 1.0

// Precision loss with large numbers
1e16 + 1  // Equals 1e16, not 1e16 + 1

// Transcendental function precision
sin(π)  // ≈ 1.2e-16, not exactly 0

Recommendations

  1. Never compare floats for exact equality — use tolerance-based comparisons
  2. Check for special values before using results in further calculations
  3. Be aware of catastrophic cancellation when subtracting similar magnitudes

Evaluation Order

Guaranteed Properties

PropertyGuarantee
Operator precedenceStrictly follows documented precedence table
AssociativityLeft-to-right for +, -, *, /; right-to-left for ^
Short-circuitNot applicable (all operands are evaluated)
Argument evaluationLeft-to-right for function arguments

Sub-expression Evaluation

Within a single expression, sub-expressions at the same precedence level are evaluated left-to-right:

dart
// Guaranteed order: f() called before g()
evaluate('f(x) + g(x)', {'x': 1.0})

Caching Effects

With caching enabled, identical sub-expressions may return cached results:

dart
// Both x^2 terms may share a cached result
evaluate('x^2 + x^2', {'x': 3.0})  // = 18.0

This does not affect the final result, only performance.


Simplification Rules

Pattern-Based Simplification

TeXpr applies local pattern-based simplification, not canonical form transformation.

PatternSimplificationApplied
x + 0xAlways
0 + xxAlways
x * 1xAlways
1 * xxAlways
x * 00Always
0 * x0Always
x ^ 01For x ≠ 0
x ^ 1xAlways
x - x0Same expression
x / x1For x ≠ 0
--xxAlways

Trigonometric Identities

These identities are applied during symbolic operations:

IdentityApplied
sin²(x) + cos²(x) = 1During simplification
sin(0) = 0During evaluation
cos(0) = 1During evaluation
tan(0) = 0During evaluation

What Is NOT Simplified

IMPORTANT

TeXpr does not produce canonical forms. These expressions are NOT recognized as equivalent:

dart
'2x + 3x'     // NOT simplified to 5x
'x + y + x'   // NOT simplified to 2x + y
'(x+1)^2'     // NOT expanded to x^2 + 2x + 1 (unless explicitly expanded)

Use the SymbolicEngine.simplify() or expand() methods for more aggressive transformations.


Numerical Integration

Simpson's Rule Parameters

ParameterValueNotes
Intervals10,000Fixed, not configurable
Error bound~O(h⁴)For smooth functions
Typical accuracy10+ decimal placesFor well-behaved functions

Improper Integral Handling

BoundReplacementNotes
100.0May be inaccurate for slow-converging functions
-∞-100.0May be inaccurate for slow-converging functions

Limitations

  • Singularities: Integrals crossing vertical asymptotes produce incorrect results
  • Oscillating functions: May not converge properly near infinity
  • Slow convergence: Functions like 1/x near infinity need larger bounds

Limit Evaluation

Algorithm

  1. Finite limit: Direct substitution of target value
  2. Limit at infinity: Evaluate at [10², 10⁴, 10⁶, 10⁸], return last stable value
  3. No L'Hôpital's rule: Indeterminate forms are not symbolically resolved

Behavior

dart
lim_{x→0} sin(x)/x     // = 1.0 (numerical convergence)
lim_{x→∞} 1/x          // = 0.0 (evaluated at large values)
lim_{x→0} 1/x          // = Infinity (direct substitution)

Caching Determinism

Cache Keys

Cache keys are computed from:

  • Expression structure (AST hash)
  • Variable bindings (sorted by name)
  • Numeric values (exact IEEE 754 representation)

Guarantees

PropertyGuarantee
Same input → same resultYes, within a single evaluator instance
Cache hit → identical resultYes
Cross-instance consistencyYes, for same expression and variables

Edge Cases

ScenarioBehavior
NaN in expressionsCache may produce unexpected hits
-0.0 vs +0.0Distinct cache keys
Floating-point roundingMay cause cache misses for "equivalent" values

Cross-Platform Consistency

Dart VM vs JavaScript

AspectConsistency
Integer arithmeticIdentical
Basic float opsGenerally identical
Transcendental functionsMay differ by 1-2 ULP
Special value handlingIdentical

Recommendations

  1. Use tolerance-based comparisons for cross-platform testing
  2. Avoid relying on exact bit patterns for numerical results
  3. Cache behavior is deterministic within a platform

Random Number Generation

TeXpr does not use random numbers in any evaluation path. All operations are deterministic given the same input.


Thread Safety

CAUTION

TeXpr evaluator instances are NOT thread-safe.

OperationThread Safety
Parse (stateless)Safe when using separate tokenizer instances
EvaluateUnsafe (mutable cache state)
DifferentiateUnsafe (uses shared evaluator)

Recommendations

  • Create one Texpr instance per thread/isolate
  • Or disable caching for shared instances (performance penalty)

Versioning and Stability

Behavioral Guarantees

AspectStability
Valid expression parsingStable within major versions
Operator precedenceStable within major versions
Numeric resultsStable for same inputs
Error typesMay add new types in minor versions
Error messagesMay change without notice

Breaking Changes

The following would require a major version bump:

  • Changing operator precedence
  • Changing associativity rules
  • Removing support for currently-supported syntax
  • Changing the exception hierarchy

Summary

PropertyTeXpr Behavior
Floating-pointIEEE 754 binary64
Special valuesInfinity, -Infinity, NaN propagated
Evaluation orderDeterministic, left-to-right at same precedence
SimplificationPattern-based only, not canonical form
IntegrationSimpson's Rule, 10k intervals
LimitsNumerical, no L'Hôpital
CachingDeterministic within instance
Thread safetyNot thread-safe
Cross-platformGenerally consistent, ±1-2 ULP for transcendentals

Made with ❤️ by TeXpr, Docs Inspired by ElysiaJS