Math Expression Calculator
Instantly evaluate complex mathematical expressions with functions, parentheses, and exponents, all within your browser.
eval. The entered expression is calculated only within the browser and is not sent to the server, and history is also saved only in this browser.
What is the Math Expression Calculator?
Solve complex math problems in a single step. This online calculator evaluates expressions with parentheses, exponents, and standard arithmetic, plus scientific functions like `sin`, `cos`, `log`, and constants like `pi`. Simply type an expression like `sqrt(16) + 2 * (3+4)^2` and see the result appear in real timeโno need to press a 'calculate' button. All calculations run entirely in your browser; your expressions are never sent to a server, ensuring your data remains private. It's free, requires no installation, and works instantly.
How to use
- Type or paste your equation into the "Enter expression" field.
- The calculation updates live, and the final answer appears in the "Result" field below.
- For trigonometric functions, first select your angle unit using the "DEG/RAD" toggle.
- Use the shortcut buttons like `pi`, `sqrt(`, and `^` to quickly add them to your expression.
- Press the Enter key to save the current expression and its result to the "Calculation History".
- Click any entry in the "Calculation History" to load it back into the calculator.
Math Expression Calculator guide
How this tool is used in real work, and what to watch out for.
Why This Calculator Uses a Custom Parser
The shortest way to compute a string like "2 * (3 + 4) ^ 2" is a single line of code: eval(expression). But this one line is a notorious security hole. That's because eval doesn't just calculate expressionsโit executes any string as JavaScript code.
The moment you accept user input, your calculator becomes an "arbitrary code executor." In a browser, a user could run code to scrape cookies and login tokens and send them to an external server (XSS). If a calculator API on a server were built with eval or new Function, it would be even more catastrophicโit could read files, execute commands, and completely take over the server. A simple calculator feature would create a remote code execution vulnerability.
That's why this tool uses neither eval nor new Function. It breaks the expression down into tokens (with a tokenizer), organizes the order of operations using the Shunting-yard algorithm, and then calculates the result using only allowed functions and operators. If it encounters an undefined name, it throws an error instead of executing itโwhich is why entering alert(1) is rejected with an "Unknown name" error.
Try Entering 0.1 + 0.2
Enter 0.1 + 0.2 into this calculator, and you'll get 0.3. But if you do the same calculation in your browser's console, you'll see 0.30000000000000004. Is the calculator wrong? Noโboth computed the same value. This tool just polishes the final number before displaying it.
The cause is how computers store decimal numbers in binary. The decimal 0.1 becomes a repeating fraction in binary, so it has to be truncated at some point. This tiny error becomes apparent during addition. This isn't just a JavaScript problem; it happens in almost every language that uses floating-point numbers. This tool just hides the noise by formatting the result to 12 significant digits, but the internal calculation error is still there.
So, How Should You Calculate Money?
There is one main principle: handle money as integers, not decimals. Perform calculations using the smallest currency unit (like cents), and only apply explicit rounding or truncation rules when division is necessary.
Value-added tax (VAT) is a classic example. If you need to find the pre-tax price from a total of $290.00 with 10% tax, you get 290 / 1.1 = 263.6363... Whether to round this up or truncate it isn't a math problemโit's a business and tax regulation problem. A calculator can't decide that for you; the rule must be centralized in your code.
- In your database, use DECIMAL or NUMERIC columns instead of FLOAT or DOUBLE. Using FLOAT for money is a common but dangerous mistake.
- When handling money in JavaScript, convert amounts to integers (e.g., cents) or use a dedicated library.
- Don't accumulate the raw results of division. Finalize the rounding rule at each step before proceeding to the next calculation.
- This calculator is great for sanity checks and estimations. Don't run your actual accounting logic on it.
Five Common Syntax Gotchas
The error message will tell you the approximate character position of the problem, but the issues below can be tricky to diagnose if you don't know what to look for.
| Input | Result | Reason |
|---|---|---|
| 2(3+4) | Error | The multiplication operator cannot be omitted. Use 2*(3+4). |
| 30 % 5 | 0 | % is the remainder (modulo) operator, not percentage. For a 30% discount, use 30/100. |
| 2^3^2 | 512 | Exponentiation is right-associative. This is evaluated as 2^(3^2) = 2^9. |
| -2^2 | -4 | Exponentiation has higher precedence than the unary minus, so this is interpreted as -(2^2). |
| log(100) | 2 | log is the common logarithm (base 10). For the natural logarithm, use ln(x). |
Large Number Limits and History
If a result exceeds 9,007,199,254,740,991 (2^53 - 1), a warning appears: 'Value exceeds 2^53, so the last digit is an approximation'. This is because JavaScript's standard number format cannot accurately represent integers larger than this. Calculations like 2^100 will produce a value, but it won't be an exact integer. For arbitrary-precision arithmetic, use a language like Python or an environment that supports BigInt.
Pressing Enter after entering an expression saves it to the Calculation History. It stores up to 10 entries. Clicking an item in the history reloads that expression into the input box, which is handy for repeating similar calculations with different values.
- History and your last entry are saved only in this browser. They are not sent to the server and will not be visible on other devices.
- If you've calculated sensitive numbers on a public computer, be sure to click "Clear History" when you're done.
- Undefined calculations like division by zero, the square root of a negative number, or tan(90ยฐ) are caught as errors instead of producing strange values like Infinity or NaN.
- The chip buttons (pi, sqrt(, round(, etc.) insert text directly at your cursor's position. You don't need to memorize the function names.
Frequently asked questions
What functions and operators are supported?
It handles arithmetic (+, -, *, /), exponents (^), and parentheses. It also supports functions like `sqrt`, `log`, `ln`, `sin`, `cos`, `tan`, `abs`, `round`, and constants like `pi` and `e`. See the "Supported Functions ยท Constants" table for a complete list.
Are my calculations and data secure?
Yes. This tool is designed for privacy. All calculations are processed directly in your web browser. Nothing you enter is sent to a server, and the "Calculation History" is stored only on your local device.
How do I calculate with degrees instead of radians?
Use the "DEG/RAD" toggle above the input field. Select "DEG" to work with degrees, where `sin(90)` will correctly yield `1`. When "RAD" is selected, all trigonometric function inputs are treated as radians.
Why is my result shown in scientific (e) notation?
Results are shown in scientific notation (e.g., `1.23e+15`) when they are very large or small, for accuracy and readability. Note that due to standard floating-point limits, integers above ~9 quadrillion (2^53) may be approximations.