Skip to main content

A fast and secure mathematical expression evaluator.

Project description


Math Engine

Math Engine is a high-precision, modular calculation engine written in pure Python.
It provides a complete processing pipeline:

tokenizing → parsing (AST) → evaluating → optional equation solving

The package is available on PyPI:

pip install math-engine

✨ Features

  • Tokenizer Converts raw text to tokens: numbers, operators, parentheses, variables, functions.

  • Parser (AST) Recursive-descent parser with correct operator precedence.

  • Evaluator

    • High-precision Decimal arithmetic (precision = 50)
    • Fraction support where needed
    • Scientific functions: sin, cos, tan, log, , π, e, etc.
    • Supports variables via dictionary injection evaluate("2+LEVEL", {"LEVEL": 0.5})
  • Equation Solver

    • Solves linear equations with one variable
  • Settings system

    • Configurable decimal places
    • Output formats
    • Scientific mode
    • Robust load/save system via config_manager
  • Custom errors with clean messages.


🚀 Quick Start

1. Basic Evaluation

from math_engine import evaluate

print(evaluate("3 * (2 + 5) - 4^2")) # -> 5

2. Comparison

from math_engine import evaluate

print(evaluate("((3 * 3) / 2) = 7 + 5")) # -> False

3. Using Variables

You can inject any number of variables:

from math_engine import evaluate

test_vars = {
    "LEVEL": 0.5,
    "ENABLED": 1,
}

expr = "2 + 2 - LEVEL - 1"
result = evaluate(expr, test_vars)

print(result)     # -> 2.5 (depending on formatting settings)

Useful for:

  • config systems
  • scripting
  • modding tools
  • dynamic calculations

4. Solving Equations

from math_engine import evaluate

solution = evaluate("3x + 5 = 11")
print(solution)   # -> 2 (for x = 2)

5. Scientific Functions

from math_engine import evaluate

print(evaluate("sin(3π/4)")) # -> 0.71
print(evaluate("√(81)"))     # -> 9
print(evaluate("log(100)"))  # -> 4.61

⚙️ Settings: Load / Modify / Save

Math Engine includes a built-in settings system (config_manager) to control:

  • Decimal precision
  • Fraction vs Decimal output
  • Thousands separators

Load all existing settings

from math_engine import load_all_settings

print(load_all_settings())

This loads e.g.:

{
    "decimal_places": 2,
    "use_degrees": false,
    "allow_augmented_assignment": true,
    "fractions": false
}

Load specific existing settings

from math_engine import load_one_setting

print(load_one_setting("decimal_places")) # -> 2

Modify / save settings in code

from math_engine import change_setting

change_setting("decimal_places", 10)  # -> 1 (for saved successfully)
change_setting("decimal_places", "a") # -> -1 (for  not saved successfully)

Use settings when evaluating

If your engine exposes something like:

from math_engine import evaluate, load_all_settings

settings = load_all_settings()

print(evaluate("1/3"))

This allows:

  • consistent output across UI/CLI
  • user-customizable formatting
  • integration with your calculator GUI

(If your package exposes the settings differently, I can adapt this section.)


📁 Project Structure

math_engine/
├── MathEngine.py        # Core tokenizer, parser, evaluator, solver
├── ScientificEngine.py  # Scientific functions
├── config_manager.py    # Load + save user settings
├── error.py             # Custom error classes
└── config.json          # Saves all the settings
└── __init__.py          # Exports evaluate(), solve(), MathEngine, etc.


🛠️ Development

git clone https://github.com/JanTeske06/math_engine
cd math_engine
pip install -e .

Run tests:

pytest

📜 License

This project is licensed under the MIT License - see the dedicated LICENSE file for details.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

math_engine-0.1.5.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

math_engine-0.1.5-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file math_engine-0.1.5.tar.gz.

File metadata

  • Download URL: math_engine-0.1.5.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for math_engine-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9520bc484ade9e9061657c7ecb6ee174ef85e7ae5801ab2843c0eef98a8b21da
MD5 3ff36f7c4af8f6bdd31ca9d39ac3845f
BLAKE2b-256 b17aa4ac3e6a05616d869564baa4e4f5e79013eb5aa86051efe5cd51b07e250b

See more details on using hashes here.

File details

Details for the file math_engine-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: math_engine-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for math_engine-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e033f141669b2361432380862691a1fcdbc66a9519e308b27ebe57f8cd1bc317
MD5 ecb7f6ab362b7f031780516e2c71a165
BLAKE2b-256 e271a3c066acc210884c2aeeafd053ed8e340d9bba8ec887fd446ee4e878e670

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page