GithubHelp home page GithubHelp logo

math-expression-parser's Introduction

math-expression-parser

What we have here is an algorithm that is able to fully interpret and solve any mathematical expression. It works by parsing the mathematical expression which is received as a string, it does it recursively.

It first organizes the expression by removing spaces, doubled signs (e.g. "--") and also by replacing the constants of euler and pi. Then it solves all logarithms, then it finds all nested parenthesis and solves them recursively, then it solves all trigonometric functions, then it solves all factorials, then it solves all powers and roots, then it solves all products and quotients and finally it solves all sums and differences.

You just need to make sure the expression was typed correctly, these are the rules:

1- trigs and logs must be written in lower case, as in log(2.0, 10.0), in which 2.0 is the base, or as in sin(4.5), in which 4.5 is in degrees, not radians.

2- use a . to write decimal numbers, meaning you should write 1.2 and not 1,2.

3- powers must be written using the ^ operator, as in 2.0^3.0, which means 2.0 to the power of 3.0.

4- roots must be written using the r operator, as in 8.0r3.0, which means the cubic root of 8.0.

5- you can use parentheses as you wish, just make sure you don't forget to open or close a pair of parentheses, as in ((2 + x), which is missing the closing parentheses.

6- it may work most times if you type values as integers, such as 3, but it is safer to always type them as floats, such as 3.0, just in case.

Compile it with "dmd mathexpressionparser.d -m64 -O -g". Then just run it and play with it, if you do anything that results in NaN (not-a-number) then it will display a message saying you've mistyped something.

Here are some examples:

1: sec(sin(3.0) * cos(x) / tan(2.0 * x - 6)) - csc(x) + cot((x - 2.0) * 3.0)

2: (x * 100.0 - 550.0)!

3: 3.0 * log(2.0, x) - 4.5

4: 3.0 ^ x - e / (x ^ e)

5: (8.0 - x + pi) * x - ((x + 7.0) * 2.0)

6: (-x)r2.0

math-expression-parser's People

Contributors

murilomir avatar

Stargazers

Ferhat Kurtulmuş avatar  avatar Mirosmar Oliveira avatar Alef Carlos avatar  avatar  avatar Dani avatar Sofia Reis avatar Marcos Vinicius avatar Jhonatan Eduardo Rampim avatar Oesley Renner avatar Joalison Pereira avatar Carlos Henrique avatar Fabio William Conceição 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.