HandyTools Hub

Scientific Calculator

Advanced calculator with trigonometric functions, logarithms, factorial, and more.

 
0

History

No calculations yet

This is a browser-based scientific calculator with a hand-written expression parser. It handles the arithmetic you would expect from a pocket calculator plus trigonometry, logarithms, powers, roots, and factorials, and it shows the working through a visible expression line rather than a single changing number. Because the parser is custom, the tool does not call JavaScript’s eval(), and nothing you type leaves the page.

How to Use

Type directly into the display or click the on-screen keys. Both routes build the same expression string.

  1. Enter your expression, for example 3 + 4 * 2.
  2. Choose DEG or RAD for the trig functions before you evaluate.
  3. Press = or Enter. The result replaces the expression on the display; the full expression is shown above it.
  4. Use the function keys for sqrt, log, ln, log2, sin, cos, tan, and their inverses. The pi and e keys insert the constants.
  5. Clear with the C key to start a fresh calculation.

The layout is responsive, so the same keypad works on a phone screen without zooming.

Worked Examples

ExpressionModeResultWhy
18 + 24 / 6any22Division binds tighter than addition
sin(30)DEG0.530 degrees becomes pi/6 radians
sin(pi/2)RAD1A quarter turn in radians
log(1000)any3log is base 10
ln(e)any1Natural log of e
5!any1205 * 4 * 3 * 2 * 1
2^3^2any512Right-associative: 2^9
sqrt(2)any1.41421356...Irrational; displayed rounded
17 % 5any2Remainder, not percent

These examples double as a quick check that you are in the angle mode you expect. If sin(30) returns something close to 0.988 instead of 0.5, you are in RAD mode.

How the Parser Evaluates Expressions

The input is processed in three stages, none of which use eval():

  1. Tokenize — the string is split into numbers, operators, function names, constants, parentheses, and the postfix !. Spaces are ignored, so 1 + 2 and 1+2 are identical.
  2. Shunting-yard — tokens are reordered into Reverse Polish Notation using an operator-precedence stack. This is where associativity is decided.
  3. RPN evaluation — a numeric stack is consumed left to right to produce the final value.

Precedence, from loosest to tightest:

LevelOperatorsAssociativity
1+ -Left
2* / %Left
3^Right
4! (postfix)—

Unary minus is handled during tokenizing: a - at the start of an expression, or straight after (, ^, *, /, %, +, or another -, is folded into the following number. So -3^2 becomes (-3)^2 = 9. If you want the conventional -(3^2), write the parentheses yourself.

Degrees vs Radians

The DEG/RAD toggle affects only sin, cos, tan, asin, acos, and atan; arithmetic and logarithms are unaffected.

In DEG mode the tool rewrites the expression mechanically. A forward trig call gets its argument multiplied by pi / 180, and an inverse trig call gets its result multiplied by 180 / pi. That means:

  • sin(90) DEG = 1
  • cos(180) DEG = -1
  • atan(1) DEG = 45
  • sin(1) DEG ≈ 0.0174524 (1 degree, not 1 radian)

In RAD mode the functions map directly to the JavaScript Math library, so tan(pi/4) = 1 and asin(1) ≈ 1.5707963.

What Triggers an Error

The calculator reports a small set of errors instead of showing NaN or Infinity:

  • Division by zero — 5 / 0 is rejected rather than returning Infinity.
  • Factorial of a negative or non-integer — (-3)! and 0.5! both fail; 0! is fine.
  • Mismatched parentheses — an unclosed or extra ) stops evaluation.
  • Non-finite result — sqrt(-1), ln(-1), and log(0) all produce a value that is NaN or infinite, which the tool flags. There is no complex-number support here.

Two further limits are worth knowing. Results use IEEE-754 double precision, so 0.1 + 0.2 displays as 0.30000000000000004; the tool does not round to a fixed number of decimals. And function names are matched whole, so sin(30) is required rather than sin 30.

Practical Tips

  • Put an explicit * between a number and a constant or function. 2pi is not implicit multiplication: the tokenizer produces two adjacent operands and evaluation fails with Invalid expression. Write 2*pi instead.
  • Scientific notation is not accepted as input. 1e3 is read as the number 1, then the constant e, then 3, which does not evaluate. Enter 1000, or use 10^3 if you want to show the power.
  • A leading decimal point is allowed, so .5 and 0.5 produce the same result.
  • Wrap a negative base in parentheses when the power matters. The tokenizer folds a leading minus into the number, so -2^2 is treated as (-2)^2 = 4. For the usual mathematical reading, write -(2^2) to get -4.
  • Check fractions by dividing inside the expression. 22/7 returns 3.142857142857143, a familiar approximation to pi.
  • The result can be selected and copied from the display, so it is easy to paste into a document or a second calculation.

Use Cases

  • Checking homework for trig, logs, and factorials when a phone calculator app hides the working.
  • Quick engineering arithmetic where you want pi, e, log2, and sqrt on one screen.
  • Demonstrating operator precedence and the DEG/RAD conversion in a classroom, since the expression and result stay visible together.
  • Statistics and combinatorics, where factorials of moderately sized numbers are common.

Frequently Asked Questions

Which functions and constants does the calculator support?

It supports the four arithmetic operators plus modulo (%), exponentiation with ^, square root (sqrt), factorial (!), base-10 log (log), natural log (ln), base-2 log (log2), and the trig functions sin, cos, tan together with their inverses asin, acos, atan. The constants pi and e are available, and log2 is recognized as its own function rather than log(2).

Why does sin(30) give 0.5 in DEG mode but a different answer in RAD mode?

DEG mode rewrites the argument before evaluation: sin(30) becomes sin(30 * pi / 180), which is sin(pi/6) = 0.5. RAD mode passes 30 straight through, and 30 radians is roughly 0.988. Inverse functions convert the other way, so asin(0.5) returns 30 in DEG mode and about 0.5236 in RAD mode.

Is 2^3^2 evaluated as (2^3)^2 or 2^(3^2)?

Right to left, as in standard mathematical notation. The exponent operator is right-associative, so 2^3^2 is parsed as 2^(3^2) = 2^9 = 512, not 64. Every other binary operator is left-associative.

Does the % button calculate a percentage?

No. % is the modulo (remainder) operator, so 17 % 5 = 2. To take a percentage of a value, write it as a fraction: 200 * 15 / 100 = 30.

Why does 0.5! return an error?

Factorial is only defined here for whole, non-negative numbers. 0.5! fails because it is not an integer, and (-3)! fails because factorial of a negative number is undefined. 0! is allowed and returns 1.

Do I need parentheses after function names like sin or sqrt?

Yes, always use them. sin(90) is unambiguous, but sin90 leaves the function on the operator stack, and in a longer expression such as sin30+2 the function captures the entire sum, producing sin(32). Write every function as name(argument).

Reviewed by the HandyTools Hub team · Last updated