Skip to main content

Mathematical Expression Evaluator (math-evaluate)

By mhaxanali

https://pypi.org/project/math-evaluate

About the project

A simple command-line calculator for quick math without leaving the terminal.

Installation

Install using:

pip install math-evaluate

Usage

Example usage:

math "10*(5-2)^2 / 3"

Output: 30

Using the Evaluator for your own Projects

In order to use the mathematical expression evaluator in your own projects, follow these steps:

  • Set up a virtual environment in your project folder (Optional):
    python -m venv venv
    source venv/bin/activate # for macOS/Linux
    venv\Scripts\activate.bat # for windows CMD
    .\venv\Scripts\Activate.ps1 # for windows Powershell
    
  • Install math-evaluate using pip:
    pip install math-evaluate
    
  • Now, in your code add this line:
    from math_evaluate.evaluator import evaluate
    
  • Now, you can call the evaluator function to return a string which can be an error message or result while giving it the mathematical expression as argument:
    result = evaluate("5+5")
    print(result, type(result))
    
    Output: 10 <class 'str'>

Testing

  • Unittests have been included in ./tests.py
  • Running tests:
    1. Clone the repository:
      git clone https://github.com/mhaxanali/math-evaluate
      
    2. Navigate to the repository:
      cd math-evaluate
      
    3. Run the tests:
      python tests.py
      

Notes

  • Supported operators: +, -, *, /, (, ), ^
  • This evaluator follows BODMAS/PEMDAS.
  • Division and multiplication are evaluated left to right, same for addition and subtraction.
  • Exponent associativity is supported.
  • Use brackets () if you want to force evaluation order.
  • Requires Python 3.10 or later.
  • Argument Parsing might glitch when using zsh if you try to evaluate an expression which contains -(.
    • If this happens then run the script using:
      math --eval="<expression>"
      
  • The CLI command is installed as math.

Metadata

Release files for math-evaluate 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for math-evaluate 0.2.0
File Size Uploaded
math_evaluate-0.2.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for math-evaluate 0.2.0
File Interpreter ABI Platform
math_evaluate-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / math_evaluate-0.2.0.tar.gz

Download URL math_evaluate-0.2.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d4a9bf645ed5aefe4aa3c30d8a5909a52e1f110b58360c44a0870e01072830e8
BLAKE2b-256 checksum
How to use checksums
c415f96eff6cc2fa7bd2bcd0eca906358afe66655e53d334b7cee7034ccba0a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / math_evaluate-0.2.0-py3-none-any.whl

Download URL math_evaluate-0.2.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b57d450a30fc55d505922d8db098e670ca0f2682d47dd0f940719506deaf78a3
BLAKE2b-256 checksum
How to use checksums
8d288db5eaf3e2d827fac58678e10bf923c2c5290082f29161e4d206e064f020
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page