Giter Site home page Giter Site logo

strmath's Introduction

tests PyPI - Downloads License CodeFactor GitHub release

strmath

A module for evaluating math expressions without eval(). Currently the module supports only simple math operations (eg. multiplication, subtraction, division, %) but in the future there will be functions support.

Installation

The module is available for installation from PyPI

$ pip install strmath

Basic Usage

from strmath import evaluate


result = evaluate("(90 + 2) // 4")
print(result)

Accuracy

As tests show, the library is 100% accurate with python native evaluation:

+----------------+--------+---------+-----+-----------+-------------+
|                | Python | StrMath | PEE | Mathparse | InfixParser |
+----------------+--------+---------+-----+-----------+-------------+
| Failures       | 0      | 0       | 61  | 150       | 91          |
| Failures (%)   | 0%     | 0%      | 30% | 75%       | 45%         |
+----------------+--------+---------+-----+-----------+-------------+

In the test above, 198 randomly generated samples were submitted to Python eval() and several other parsing libraries, including strmath. As you can see, the library has 0 failures and almost same speed with native python. You can see test implementation here.

Features

Currently Supported

  • basic math operations (+, -, /, //, *, **, %) eg. "2 + 5 * 2" --> 12
  • float operations
  • braces eg. "(2 + 5) * 2" --> 20
  • functions from math module and some builtins

Planned for Future

  • custom functions registration
  • correct - operator as neg (eg. 50+-30)
  • mathematical constants (i.e. PI)

Principle

  1. Tokenize expression (split it into operators and numbers)
  2. Apply operations order
  3. Build evaluation binary tree
  4. Evaluate the tree

License

This repository is licensed under MIT license. See LICENSE for details.

strmath's People

Contributors

exenifix avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.