Scientific Calculator
A keyboard-first scientific calculator with full operator precedence, degrees or radians, memory keys, history and readable error messages — all evaluated locally in your browser.
! after a number for a factorial, % for percent and the rest of the
expression as ordinary text — nothing you type leaves this page.
How this calculator works
The expression you type is read by a small parser written from scratch for this page: a tokenizer
turns the characters into numbers, constants, function names and operators, and a recursive-descent
parser builds a tree from those tokens. The tree is then evaluated in one pass. No eval
or new Function is involved anywhere, so an expression cannot run arbitrary script — it
can only produce a number or a readable complaint such as Cannot divide by zero or
Unbalanced parentheses. Anything that fails, whether a syntax error or a mathematical
domain error, leaves the display untouched so you can fix the expression and try again.
Because the parser understands structure rather than text, functions compose naturally.
sqrt(16) + sin(30) is evaluated as two independent values added together, and
ln(e^2) returns 2 exactly as the mathematics predicts. Factorials, powers and percent
signs are treated as postfix or right-associative operators at the right precedence, so
2^3^2 is 512 rather than 64. Every step stays inside your browser tab.
Around the parser sits the usual calculator furniture: a memory register with MS, MR, M+ and MC, a ten-entry history you can click to reload a previous result, a Deg/Rad switch for trigonometry, and a result line that previews the answer while you type. The display is a genuine text field, so the on-screen keypad is a convenience rather than a requirement.
Operator precedence and order of operations
Input is grouped from the tightest-binding operators outward, following the standard conventions
for written mathematics. Power binds tighter than the unary minus on its left, which is why
-2^2 evaluates to -4 — the exponent is applied first and the sign afterwards. If you
want the square of negative two, write (-2)^2.
| Level | Operators | Associativity | Example |
|---|---|---|---|
| 1 (highest) | Function calls sin(x), sqrt(x), parentheses | — | sqrt(9) × 2 = 6 |
| 2 | Postfix factorial ! and percent % | Left to right | 5! = 120, 50% = 0.5 |
| 3 | Power ^ | Right to left | 2^3^2 = 512 |
| 4 | Unary minus -5, +5 | Right to left | -3! = -6 |
| 5 | Multiplication *, division /, remainder mod | Left to right | 8 / 4 × 2 = 4, 17 mod 5 = 2 |
| 6 (lowest) | Addition +, subtraction - | Left to right | 2 + 3 × 4 = 14 |
The percent sign is the one operator whose meaning depends on where it sits. When it follows a
value that is being added to or subtracted from something else, it is read as a share of that
running total: 200 + 10% is 220 and 200 - 10% is 180. In every other
position it simply divides by one hundred, so 50% × 80 is 40 and 200 × 10%
is 20. The result line prints the expanded arithmetic so the interpretation is never hidden.
Two consequences are worth knowing. A percent sign binds tighter than multiplication, so
7%3 is 0.21 — write (7%)3 if that is what you meant, or reach for the
remainder operator. And when both sides of a plus or minus carry a percent sign, the left value
scales the right one: 10% + 10% is 0.11 rather than 0.2, because the second ten percent
is taken as a share of the first. To find the remainder of a division, write the word
mod between the values. It follows the JavaScript remainder rule, where the sign of the
answer matches the left operand: -7 mod 3 is -1.
Degrees vs radians
The Radians button in the display header toggles the unit used by
sin, cos, tan and their inverses asin,
acos and atan. The badge next to it shows the active mode — DEG
or RAD — so you can always tell at a glance which one is in force. The setting is saved
in your browser and restored the next time you open the page.
In degree mode the calculator converts on the way in and on the way out: an angle is multiplied by
π/180 before it reaches the trigonometric function, and the result of an inverse function is
multiplied by 180/π. That is what makes sin(30) return exactly 0.5 and
asin(0.5) return 30. In radian mode the numbers pass through untouched, matching the
behaviour of Math.sin and friends, so sin(π/6) is the expression that
gives 0.5. Radians are the default in most programming contexts; degrees are what most people reach
for when they are working from a measurement.
Rule of thumb: if you are converting an angle you measured by hand, you want degrees. If you are checking code, a physics formula or a graph that uses π, you want radians. The toggle changes only the trigonometry — logarithms, roots, powers and everything else behave identically in both modes.
Keyboard shortcuts
The display accepts ordinary text input, so you can work without ever touching the mouse. Click into the display once, or simply start typing — the page listens for keys while the cursor is in the expression field.
| Key | Action |
|---|---|
| 0–9 and . | Insert a digit or a decimal point at the caret |
| + - * / ^ % | Insert an operator; % is a postfix percent, so 50% is read as 0.5 |
| ( ) | Insert a parenthesis |
| Enter | Evaluate the expression and push it to the history list |
| Esc | Clear the expression (restores what you had if you press it once more) |
| Backspace | Delete the character before the caret, or the current selection |
| ← → Home End | Move the caret so you can edit part of a long expression |
| Ctrl+A, Ctrl+C, Ctrl+V | Select, copy and paste the expression as normal text |
pi or π, e, sqrt(, sin( | Typed names work as well as the keypad: π, Euler's number and every function button |
mod | Remainder of a division, as in 17 mod 5 |
Accuracy notes
Numbers here are JavaScript numbers: IEEE 754 binary64 double precision, which gives about 15 to
17 significant decimal digits and a finite set of exactly representable values. Simple decimals
such as 0.1 have no exact binary form, so 0.1 + 0.2 really does produce
0.30000000000000004 internally. The display rounds to 12 significant digits, deliberately a little
short of what a double can hold, so that this kind of noise stays invisible in everyday arithmetic
while genuine differences still show up.
Rounding is applied for display only; the underlying value keeps full precision, and results that
feed into further work (memory and history) are stored unrounded. Very large or very small answers
switch to exponent form — 1.2e+21 — and anything beyond the range of a double becomes
Result is too large rather than silently turning into Infinity. Trigonometry
at multiples of 90° is the other place where tiny discrepancies surface: sin(180) may
come back as 1.2e-16 instead of a clean zero, because π itself cannot be represented exactly. Treat
results near zero in such cases as zero.
For money and any other quantity that must balance to the cent, do not rely on floating-point arithmetic. Work in integer units — cents, or the smallest indivisible amount — and convert to a decimal only when you are ready to display the final figure.
Frequently asked questions
Why does sin(30) return 0.5 in degree mode but a different number in radian mode?
Degrees and radians are two ways of measuring the same angle. 30° is π/6 radians, which is about 0.5236. The trigonometric functions built into the browser expect radians, so the calculator converts your angle before calling them when the Deg toggle is active: it multiplies by π/180 on the way in and by 180/π on the way out for asin, acos and atan. If you leave the toggle on Rad, sin(30) means the sine of 30 radians, which is roughly -0.988.
What does the percent key do?
It depends on the context, exactly like a pocket calculator. After + or -, a percent sign is read as a relative share of the running total: 200 + 10% computes 200 + (200 × 0.10) = 220, and 200 - 10% computes 180. Everywhere else it converts the number on its left to a plain fraction of one: 50% × 80 = 40 and 200 × 10% = 20. The line under the display always shows the expanded expression, so you can read back what was actually calculated. For the remainder of a division, type the word mod instead, as in 17 mod 5.
How do I type the expression from my keyboard instead of clicking?
The display is a real text field, so the keypad is optional. Type digits and + - * / ^ ( ) . directly, and use the arrow keys to move the caret when you want to patch an existing expression. Enter evaluates, Escape clears the field, and Backspace deletes one character to the left of the caret. If you would rather not type function names, press the on-screen sin, cos or sqrt buttons — they insert at the caret position and keep your place. For powers you can also press the xʸ key, and for a factorial press n! or type a ! after the number.
Why does 0.1 + 0.2 not show 0.3?
Because JavaScript numbers are IEEE 754 double-precision binary floating point, and 0.1 and 0.2 have no exact binary representation — the same limitation every browser, spreadsheet and programming language has. The sum is 0.30000000000000004, and this calculator rounds the display to 12 significant digits, just short of the 15 to 17 digits a double can carry, which hides almost all of that noise. For money, work in whole cents or integer units and divide at the end.
Sources & further reading
- MDN Web Docs — Math — Documents every Math function this calculator wraps, including the fact that sin, cos and tan take radian arguments.
- IEEE 754-2019 — Standard for Floating-Point Arithmetic — The specification behind the binary64 format used by JavaScript numbers, and therefore behind the rounding behaviour described in the accuracy notes.
- Wikipedia — Order of operations — Explains the standard precedence rules, including the ambiguity of the unary minus before a power that this parser resolves explicitly.