Last Updated: June 4, 2020

# Math Calculator

## Introduction

Imagine a calculator that is not constrained to under 16 significant digits. Dream no longer, for this calculator will perform most scientific functions to hundreds of significant digits. This was too good to keep to ourselves, so please enjoy.

Input:

Output:

# Constants▼

## Named Constants▼

The following named constants are available:

Name(s) Approximate value
`e` 2.718281828459045…
`π` `pi` 3.141592653589793…
`τ` `tau` 6.283185307179586…

## Numeric Constants▼

A numeric constant can have an integer part, a fractional part, or both, optionally followed by an exponent part:

• an integer part consists of one or more decimal digits

• a fractional part consists of a radix point `.` followed by one or more decimal digits

• an exponent part consists of `E` or `e`, optionally followed by `+` or `-`, followed by one or more decimal digits

Examples
`1 ` `.5 ` `1.5 `
`1e2 ` `.5e2 ` `1.5e2 `
`1E2 ` `.5E2 ` `1.5E2 `
`1e+2` `.5e+2` `1.5e+2`
`1E+2` `.5E+2` `1.5E+2`
`1e-2` `.5e-2` `1.5e-2`
`1E-2` `.5E-2` `1.5E-2`

# Operators▼

The following operators are available for use in expressions:

Category Precedence Associativity Operator Description Example
Primary highest none `()` Subexpression `(1 + e)`
Postfix 2nd highest left to right `()` Function `lcm(3, 4)`
`!` Factorial `4!`
Power 3rd highest right to left `^` Exponentiation `2^6`
Prefix 4th highest right to left `+` Unary plus `+3`
`-` Negation `-7`
`√` Square Root `√2`
Multiplicative 5th highest left to right   Implicit multiplication `2pi`
`*` Explicit multiplication `2 * pi`
`/` Division `pi / 2`
`%` Remainder `12 % 5`
Additive lowest left to right `+` Addition `e + 1`
`-` Subtraction `e - 1`

When a named constant is immediately followed by a numeric constant or named function, implicit multiplication is not recognizable. For example, `pi2` and `esin4` are in error, whereas `2pi`, `pi 2`, and `e sin 4` are valid.

# Functions▼

The following functions are available for use in expressions. Function names are case sensitive.

The operand of a unary function need not be parenthesized. For example, `ln 2` and `sin sqrt 2` are valid. When used in this manner, the function name behaves as a prefix operator. For example, `sin 2π` is parsed as (sin 2) × π and `ln 2 ^ 4` is parsed as ln(24).

## Rounding▼

Function Description
`ceil(x)` returns the smallest integer not less than x
`floor(x)` returns the largest integer not greater than x
`int(x)` returns the integer portion of x
`round(x)` returns the integer value nearest to x
(ties are rounded away from zero)
`trunc(x)` returns the integer portion of x

## Roots, Exponents, and Logarithms▼

Function Description
`cbrt(x)` returns the cube root of x
`exp(x)` returns ex
`exp2(x)` returns 2x
`exp10(x)` returns 10x
`ln(x)` returns the natural logarithm of x
`log(x, b)` returns the base b logarithm of x
`log2(x)` returns the base 2 logarithm of x
`log10(x)` returns the base 10 logarithm of x
`sqrt(x)` returns the square root of x

## Trigonometry▼

Function Description
`acos(x)` returns the arccosine of x
`acosh(x)` returns the inverse hyperbolic cosine of x
`acot(x)` returns the arccotangent of x
`acoth(x)` returns the inverse hyperbolic cotangent of x
`acsc(x)` returns the arccosecant of x
`acsch(x)` returns the inverse hyperbolic cosecant of x
`asec(x)` returns the arcsecant of x
`asech(x)` returns the inverse hyperbolic secant of x
`asin(x)` returns the arcsine of x
`asinh(x)` returns the inverse hyperbolic sine of x
`atan(x)` returns the arctangent of x
`atan2(y, x)` returns the two-argument arctangent of y and x
`atanh(x)` returns the inverse hyperbolic tangent of x
`cos(x)` returns the cosine of x
`cosh(x)` returns the hyperbolic cosine of x
`cot(x)` returns the cotangent of x
`coth(x)` returns the hyperbolic cotangent of x
`csc(x)` returns the cosecant of x
`csch(x)` returns the hyperbolic cosecant of x
`hypot(x, y)` returns the hypotenuse of x and y
`sec(x)` returns the secant of x
`sech(x)` returns the hyperbolic secant of x
`sin(x)` returns the sine of x
`sinc(x)` returns the cardinal sine of x
`sinh(x)` returns the hyperbolic sine of x
`tan(x)` returns the tangent of x
`tanh(x)` returns the hyperbolic tangent of x

## Miscellaneous▼

Function Description
`abs(x)` returns the absolute value of x
`avg(x, y)` returns the average of x and y
`combin(n, k)` returns the number of ways to select k items out of n items
(binomial coefficient)
`gcd(x, y)` returns the greatest common divisor of x and y
`hgd(k, n, K, N)` returns the probability of selecting k items out of n items,
given that K items are selected from N items
`interp(x, x0, y0, x1, y1)` returns the linearly-interpolated y value for x
given the coordinates (x0, y0) and (x1, y1)
`lcm(x, y)` returns the least common multiple of x and y
`max(x, y)` returns the maximum of x and y
`min(x, y)` returns the minimum of x and y
`nabs(x)` returns the negated absolute value of x
`permut(n, k)` returns the number of ways to arrange k items out of n items
`sign(x)` returns −1, 0, or +1 according to the value of x

# Syntax▼

The following EBNF-like notation describes the expression syntax:

```expression:
;

| multiplicative-expression
;

multiplicative-expression:
| prefix-expression
| multiplicative-expression prefix-expression
| multiplicative-expression '*' prefix-expression
| multiplicative-expression '/' prefix-expression
| multiplicative-expression '%' prefix-expression
;

prefix-expression:
| '+' prefix-expression
| '-' prefix-expression
| '√' prefix-expression
| unary-function prefix-expression
| power-expression
;

unary-function:
| 'ceil'    | 'floor'
| 'int'     | 'trunc'
| 'round'
| 'sqrt'    | 'cbrt'
| 'exp'     | 'exp2'    | 'exp10'
| 'ln'      | 'log'     | 'log2'    | 'log10'
| 'sin'     | 'asin'    | 'sinh'    | 'asinh'   | 'sinc'
| 'cos'     | 'acos'    | 'cosh'    | 'acosh'
| 'tan'     | 'tanh'    | 'atan'    | 'atanh'
| 'sec'     | 'asec'    | 'sech'    | 'asech'
| 'csc'     | 'acsc'    | 'csch'    | 'acsch'
| 'cot'     | 'acot'    | 'coth'    | 'acoth'
| 'abs'     | 'nabs'    | 'sign'
;

power-expression:
| postfix-expression
| postfix-expression '^' prefix-expression
;

postfix-expression:
| function
| primary-expression
| postfix-expression '!'
;

function:
| function-name '(' argument-list ')'
;

function-name:
| 'floor'   | 'ceil'
| 'int'     | 'trunc'
| 'round'
| 'sqrt'    | 'cbrt'
| 'exp'     | 'exp2'    | 'exp10'
| 'ln'      | 'log'     | 'log2'    | 'log10'
| 'hypot'
| 'sin'     | 'asin'    | 'sinh'    | 'asinh'   | 'sinc'
| 'cos'     | 'acos'    | 'cosh'    | 'acosh'
| 'tan'     | 'tanh'    | 'atan'    | 'atanh'   | 'atan2'
| 'sec'     | 'asec'    | 'sech'    | 'asech'
| 'csc'     | 'acsc'    | 'csch'    | 'acsch'
| 'cot'     | 'acot'    | 'coth'    | 'acoth'
| 'abs'     | 'nabs'    | 'sign'
| 'min'     | 'max'     | 'avg'
| 'gcd'     | 'lcm'
| 'combin'  | 'permut'  | 'hgd'
| 'interp'
;

argument-list:
| expression
| argument-list ',' expression
;

primary-expression:
| constant
| '(' expression ')'
;

constant:
| named-constant
| numeric-constant
;

named-constant:
| 'e'
| 'π' | 'pi'
| 'τ' | 'tau'
;

numeric-constant:
| integer-part [ fraction-part ] [ exponent-part ]
| fraction-part [ exponent-part ]
;

integer-part:
| digit { digit }
;

digit:
| '0' | '1' | '2' | '3' | '4'
| '5' | '6' | '7' | '8' | '9'
;

fraction-part:
| '.' integer-part
;

exponent-part:
| exponent-char [ exponent-sign ] integer-part
;

exponent-char:
| 'E'
| 'e'
;

exponent-sign:
| '+'
| '-'
;
```

uses

## Acknowledgements

We would like to thank MathJS for inspiring this calculator and some of the source code.

Written by: