How to Use the Expression Calculator
Expression Syntax Guide
Section titled “Expression Syntax Guide”The expression parser is aimed at a mathematical audience, not a programming audience. The syntax is similar to most calculators and mathematical applications. Key differences include:
- Matrix indexes are one-based instead of zero-based
- There are index and range operators which allow more conveniently getting and setting matrix indexes, like
A[2:4, 1] - Both indexes and ranges have the upper-bound included
- There is a differing syntax for defining functions. Example:
f(x) = x^2 - There are custom operators like
x + yinstead ofadd(x, y) - Some operators are different. For example
^is used for exponentiation, not bitwise xor - Implicit multiplication, like
2 pi, is supported and has special rules - Relational operators (
<,>,<=,>=,==, and!=) are chained, so5 < x < 10is equivalent to5 < x and x < 10 - The precedence of some operators is different
Operators
Section titled “Operators”The expression calculator uses conventional infix notation for operators: an operator is placed between its arguments. Round parentheses can be used to override the default precedence of operators.
Examples:
2 + 3 # Result: 52 * 3 # Result: 6
# use parentheses to override the default precedence2 + 3 * 4 # Result: 14(2 + 3) * 4 # Result: 20Operator Reference
Section titled “Operator Reference”| Operator | Description | Example | Result |
|---|---|---|---|
(, ) | Grouping | (x) | - |
[, ] | Matrix, Index | [[1,2],[3,4]] | Matrix |
{, } | Object | {a: 1, b: 2} | Object |
, | Parameter separator | max(2, 1, 5) | 5 |
. | Property accessor | obj.prop | - |
; | Statement separator | a=2; b=3; a*b | [6] |
\n | Statement separator | a=2 \n b=3 | [2,3] |
+ | Add | 4 + 5 | 9 |
+ | Unary plus | +4 | 4 |
- | Subtract | 7 - 3 | 4 |
- | Unary minus | -4 | -4 |
* | Multiply | 2 * 3 | 6 |
.* | Element-wise multiply | [1,2,3] .* [1,2,3] | [1,4,9] |
/ | Divide | 6 / 2 | 3 |
./ | Element-wise divide | [9,6,4] ./ [3,2,2] | [3,3,2] |
% | Percentage | 8% | 0.08 |
% | Addition with Percentage | 100 + 3% | 103 |
% | Subtraction with Percentage | 100 - 3% | 97 |
% mod | Modulus | 8 % 3 | 2 |
^ | Power | 2 ^ 3 | 8 |
.^ | Element-wise power | [2,3] .^ [3,3] | [8,27] |
' | Transpose | [[1,2],[3,4]]' | [[1,3],[2,4]] |
! | Factorial | 5! | 120 |
& | Bitwise and | 5 & 3 | 1 |
~ | Bitwise not | ~2 | -3 |
| | Bitwise or | 5 | 3 | 7 |
^| | Bitwise xor | 5 ^| 2 | 7 |
<< | Left shift | 4 << 1 | 8 |
>> | Right arithmetic shift | 8 >> 1 | 4 |
>>> | Right logical shift | -8 >>> 1 | 2147483644 |
and | Logical and | true and false | false |
not | Logical not | not true | false |
or | Logical or | true or false | true |
xor | Logical xor | true xor true | false |
= | Assignment | a = 5 | 5 |
? : | Conditional | 15 > 100 ? 1 : -1 | -1 |
?? | Nullish coalescing | null ?? 2 | 2 |
?. | Optional chaining accessor | obj?.prop | - |
: | Range | 1:4 | [1,2,3,4] |
to, in | Unit conversion | 2 inch to cm | 5.08 cm |
== | Equal | 2 == 4 - 2 | true |
!= | Unequal | 2 != 3 | true |
< | Smaller | 2 < 3 | true |
> | Larger | 2 > 3 | false |
<= | Smaller or equal | 4 <= 3 | false |
>= | Larger or equal | 2 + 4 >= 6 | true |
Operator Precedence
Section titled “Operator Precedence”From highest to lowest precedence:
(...) [...] {...}- Grouping, Matrix, Objectx(...) x[...] obj.prop :- Function call, Matrix index, Property accessor'- Matrix transpose!- Factorial??- Nullish coalescing^, .^- Exponentiation+, -, ~, not- Unary operators%- Unary percentage- Implicit multiplication
*, /, .*, ./, %, mod- Multiply, divide, modulus+, -- Add, subtract:- Rangeto, in- Unit conversion<<, >>, >>>- Bitwise shifts==, !=, <, >, <=, >=- Relational&- Bitwise and^|- Bitwise xor|- Bitwise orand- Logical andxor- Logical xoror- Logical or?, :- Conditional expression=- Assignment,- Parameter separator;- Row separator\n, ;- Statement separators
Lazy Evaluation:
Bitwise and logical operators use lazy evaluation when possible:
false and x # Result: false (x is not evaluated)Functions
Section titled “Functions”Functions are called by entering their name, followed by zero or more arguments enclosed by parentheses.
Examples:
sqrt(25) # Result: 5log(10000, 10) # Result: 4sin(pi / 4) # Result: 0.7071067811865475Defining Custom Functions
Section titled “Defining Custom Functions”You can define custom functions by “assigning” an expression to a function call:
Examples:
f(x) = x ^ 2 - 5f(2) # Result: -1f(3) # Result: 4
g(x, y) = x ^ yg(2, 3) # Result: 8Note on Dynamic Variables: Functions do not create closures - all variables are dynamic:
x = 7h(y) = x + yh(3) # Result: 10x = 3h(3) # Result: 6 (not 10!)Passing Functions as Parameters:
twice(func, x) = func(func(x))square(x) = x ^ 2twice(square, 2) # Result: 16
f(x) = 3*xtwice(f, 2) # Result: 18Operator Function Equivalents
Section titled “Operator Function Equivalents”Most operators have function equivalents:
| Operator | Function | Example |
|---|---|---|
a + b | add(a, b) | add(2, 3) = 5 |
a - b | subtract(a, b) | subtract(7, 3) = 4 |
a * b | multiply(a, b) | multiply(2, 3) = 6 |
a / b | divide(a, b) | divide(6, 2) = 3 |
a ^ b | pow(a, b) | pow(2, 3) = 8 |
a % b | mod(a, b) | mod(8, 3) = 2 |
a == b | equal(a, b) | equal(2, 2) = true |
a and b | and(a, b) | and(true, false) = false |
not a | not(a) | not(true) = false |
Common Functions
Section titled “Common Functions”Some commonly used functions include:
- Trigonometric:
sin(),cos(),tan(),asin(),acos(),atan() - Exponential:
exp(),log(),log10(),sqrt() - Rounding:
round(),floor(),ceil(),fix() - Statistical:
min(),max(),mean(),median(),std() - Matrix:
det(),inv(),transpose(),dot(),cross()
Methods
Section titled “Methods”Some data types have methods which can be accessed using dot notation:
Unit Methods:
a = 1 ma.toNumber("mm") # Result: 1000Matrix/Array Methods:
M = [4, 9, 25]M.map(sqrt) # Result: [2, 3, 5]Map and ForEach Functions
Section titled “Map and ForEach Functions”The map function applies a function to each element of an array:
square(x) = x ^ 2map([1, 2, 3, 4], square) # Result: [1, 4, 9, 16]map([1, 2, 3, 4], f(x) = x ^ 2) # Result: [1, 4, 9, 16]map([1, 2, 3, 4], x ^ 2) # Result: [1, 4, 9, 16]Constants and Variables
Section titled “Constants and Variables”Built-in Constants
Section titled “Built-in Constants”The calculator has the following built-in mathematical constants:
| Constant | Description | Value |
|---|---|---|
e, E | Euler’s number, base of natural logarithm | 2.718281828459045 |
pi, PI | Ratio of circle’s circumference to diameter | 3.141592653589793 |
tau | Ratio of circle’s circumference to radius (2π) | 6.283185307179586 |
phi | Golden ratio: (1 + √5) / 2 | 1.618033988749895 |
i | Imaginary unit: √-1 | i |
Infinity | Positive infinity | Infinity |
NaN | Not a number | NaN |
null | Null value | null |
undefined | Undefined value | undefined |
LN2 | Natural logarithm of 2 | 0.6931471805599453 |
LN10 | Natural logarithm of 10 | 2.302585092994046 |
LOG2E | Base-2 logarithm of e | 1.4426950408889634 |
LOG10E | Base-10 logarithm of e | 0.4342944819032518 |
SQRT1_2 | Square root of 1/2 | 0.7071067811865476 |
SQRT2 | Square root of 2 | 1.4142135623730951 |
Examples:
pi # Result: 3.141592653589793e ^ 2 # Result: 7.3890560989306495log(e) # Result: 1e ^ (pi * i) + 1 # Result: ~0 (Euler's identity)tau # Result: 6.283185307179586 (2 * pi)phi # Result: 1.618033988749895 (golden ratio)sin(pi / 4) # Result: 0.7071067811865475i * i # Result: -1SQRT2 # Result: 1.4142135623730951LN10 # Result: 2.302585092994046Physical Constants
Section titled “Physical Constants”Math.js includes numerous physical constants for scientific calculations:
Universal Constants
Section titled “Universal Constants”| Constant | Symbol | Value | Unit |
|---|---|---|---|
speedOfLight | c | 299792458 | m · s⁻¹ |
gravitationConstant | G | 6.6738480e-11 | m³ · kg⁻¹ · s⁻² |
planckConstant | h | 6.626069311e-34 | J · s |
reducedPlanckConstant | ℏ | 1.05457172647e-34 | J · s |
Electromagnetic Constants
Section titled “Electromagnetic Constants”| Constant | Symbol | Value | Unit |
|---|---|---|---|
magneticConstant | μ₀ | 1.2566370614e-6 | N · A⁻² |
electricConstant | ε₀ | 8.854187817e-12 | F · m⁻¹ |
vacuumImpedance | Z₀ | 376.730313461 | Ω |
coulomb | κ | 8.9875517873681764e9 | N · m² · C⁻² |
elementaryCharge | e | 1.60217656535e-19 | C |
bohrMagneton | μB | 9.2740096820e-24 | J · T⁻¹ |
conductanceQuantum | G₀ | 7.748091734625e-5 | S |
inverseConductanceQuantum | G₀⁻¹ | 12906.403721742 | Ω |
magneticFluxQuantum | φ₀ | 2.06783375846e-15 | Wb |
nuclearMagneton | μN | 5.0507835311e-27 | J · T⁻¹ |
klitzing | RK | 25812.807443484 | Ω |
Atomic and Nuclear Constants
Section titled “Atomic and Nuclear Constants”| Constant | Symbol | Value | Unit |
|---|---|---|---|
bohrRadius | a₀ | 5.291772109217e-11 | m |
classicalElectronRadius | re | 2.817940326727e-15 | m |
electronMass | me | 9.1093829140e-31 | kg |
fermiCoupling | GF | 1.1663645e-5 | GeV⁻² |
fineStructure | α | 7.297352569824e-3 | - |
hartreeEnergy | Eh | 4.3597443419e-18 | J |
protonMass | mp | 1.67262177774e-27 | kg |
deuteronMass | md | 3.3435830926e-27 | kg |
neutronMass | mn | 1.6749271613e-27 | kg |
quantumOfCirculation | h/(2me) | 3.636947552024e-4 | m² · s⁻¹ |
rydberg | R∞ | 10973731.56853955 | m⁻¹ |
thomsonCrossSection | - | 6.65245873413e-29 | m² |
weakMixingAngle | - | 0.222321 | - |
efimovFactor | - | 22.7 | - |
Physico-chemical Constants
Section titled “Physico-chemical Constants”| Constant | Symbol | Value | Unit |
|---|---|---|---|
atomicMass | mu | 1.66053892173e-27 | kg |
avogadro | NA | 6.0221412927e23 | mol⁻¹ |
boltzmann | k | 1.380648813e-23 | J · K⁻¹ |
faraday | F | 96485.336521 | C · mol⁻¹ |
firstRadiation | c₁ | 3.7417715317e-16 | W · m² |
loschmidt | n₀ | 2.686780524e25 | m⁻³ |
gasConstant | R | 8.314462175 | J · K⁻¹ · mol⁻¹ |
molarPlanckConstant | NA · h | 3.990312717628e-10 | J · s · mol⁻¹ |
molarVolume | Vm | 2.241396820e-10 | m³ · mol⁻¹ |
sackurTetrode | - | -1.164870823 | - |
secondRadiation | c₂ | 1.438777013e-2 | m · K |
stefanBoltzmann | σ | 5.67037321e-8 | W · m⁻² · K⁻⁴ |
wienDisplacement | b | 2.897772126e-3 | m · K |
Adopted Values
Section titled “Adopted Values”| Constant | Symbol | Value | Unit |
|---|---|---|---|
molarMass | Mu | 1e-3 | kg · mol⁻¹ |
molarMassC12 | M(¹²C) | 1.2e-2 | kg · mol⁻¹ |
gravity | gn | 9.80665 | m · s⁻² |
atm | atm | 101325 | Pa |
Natural Units
Section titled “Natural Units”| Constant | Symbol | Value | Unit |
|---|---|---|---|
planckLength | lP | 1.61619997e-35 | m |
planckMass | mP | 2.1765113e-8 | kg |
planckTime | tP | 5.3910632e-44 | s |
planckCharge | qP | 1.87554595641e-18 | C |
planckTemperature | TP | 1.41683385e+32 | K |
Examples:
speedOfLight # Result: 299792458 m / savogadro * 12 g # Result: 6.0221412927e23 * 12 gboltzmann * 300 K # Result: 4.141946439e-21 Jgravity * 70 kg # Result: 686.4655 NVariables
Section titled “Variables”Variables can be defined using the assignment operator =:
Examples:
a = 3.4 # Result: 3.4b = 5 / 2 # Result: 2.5a * b # Result: 8.5Variable Naming Rules:
Variable names must:
- Begin with a letter (a-z, A-Z), underscore (_), dollar sign ($), or Unicode letter
- Contain only letters, digits (0-9), and allowed special characters
- Not be reserved words:
mod,to,in,and,xor,or,not,end
Valid examples: x, myVar, _temp, $result, α (Greek letters are allowed)
Data Types
Section titled “Data Types”Numbers
Section titled “Numbers”Numbers use a point as decimal mark and can be entered with exponential notation:
Examples:
2 # Result: 23.14 # Result: 3.141.4e3 # Result: 140022e-3 # Result: 0.022Converting Numbers and Strings:
number("2.3") # Result: 2.3string(2.3) # Result: "2.3"Binary, Octal, and Hexadecimal:
0b11 # Result: 3 (binary)0o77 # Result: 63 (octal)0xff # Result: 255 (hexadecimal)0xffi8 # Result: -1 (with word size)Non-decimal with Radix Point:
0b1.1 # Result: 1.50o1.4 # Result: 1.50x1.8 # Result: 1.5Formatting Numbers:
format(3, {notation: "bin"}) # Result: '0b11'format(63, {notation: "oct"}) # Result: '0o77'format(255, {notation: "hex"}) # Result: '0xff'bin(-1, 8) # Result: '0b11111111i8'Floating Point Precision:
0.1 + 0.2 # Result: 0.30000000000000004 (rounding error)1e-325 # Result: 0 (underflow)1e309 # Result: Infinity (overflow)Booleans
Section titled “Booleans”Examples:
true # Result: truefalse # Result: false(2 == 3) == false # Result: trueConverting Booleans:
number(true) # Result: 1string(false) # Result: "false"boolean(1) # Result: trueboolean("false") # Result: falseBigNumbers
Section titled “BigNumbers”BigNumbers provide arbitrary precision for calculations:
Examples:
bignumber(0.1) + bignumber(0.2) # Result: 0.3 (exact)Complex Numbers
Section titled “Complex Numbers”Complex numbers can be created using the imaginary unit i:
Examples:
a = 2 + 3i # Result: 2 + 3ib = 4 - i # Result: 4 - ia + b # Result: 6 + 2ia * b # Result: 11 + 10ii * i # Result: -1sqrt(-4) # Result: 2i
# Get real and imaginary partsre(a) # Result: 2im(a) # Result: 3The calculator supports a comprehensive unit system for scientific and engineering calculations. Units can be combined, converted, and used in arithmetic operations.
Creating Units:
5.4 kg # Result: 5.4 kg45 cm # Result: 45 cm90 km/h # Result: 90 km / h2 inch # Result: 2 inch101325 kg/(m s^2) # Result: 101325 kg / (m s²) (Pascal)Unit Conversions:
2 inch to cm # Result: 5.08 cm20 celsius to fahrenheit # Result: ~68 fahrenheit90 km/h to m/s # Result: 25 m / s1 mile to km # Result: 1.609344 km5 feet to meter # Result: 1.524 mArithmetic with Units:
0.5 kg + 33 g # Result: 0.533 kg3 inch + 2 cm # Result: 3.7874 inch12 seconds * 2 # Result: 24 seconds80 mi/h * 2 tonne # Calculate kinetic energy5 m * 3 m # Result: 15 m² (area)10 N / 2 kg # Result: 5 m / s² (acceleration)Trigonometry with Angles:
sin(45 deg) # Result: 0.7071067811865475cos(pi rad) # Result: -1tan(1 rad) # Result: 1.557407724654902345 deg to rad # Result: 0.7853981633974483 radUnit Conversion to Numbers:
number(5 cm, mm) # Result: 50(5 cm).toNumber('mm') # Result: 50(1 hour).toNumber('s') # Result: 3600Supported Units
Section titled “Supported Units”| Category | Units |
|---|---|
| Length | meter (m), inch (in), foot (ft), yard (yd), mile (mi), link (li), rod (rd), chain (ch), angstrom, mil |
| Area | m², sqin, sqft, sqyd, sqmi, sqrd, sqch, sqmil, acre, hectare |
| Volume | m³, litre (l, L, lt, liter), cc, cuin, cuft, cuyd, teaspoon, tablespoon |
| Liquid Volume | minim, fluiddram (fldr), fluidounce (floz), gill (gi), cup (cp), pint (pt), quart (qt), gallon (gal), beerbarrel (bbl), oilbarrel (obl), hogshead, drop (gtt) |
| Angles | rad (radian), deg (degree), grad (gradian), cycle, arcsec (arcsecond), arcmin (arcminute) |
| Time | second (s, secs, seconds), minute (min, mins, minutes), hour (h, hr, hrs, hours), day (days), week (weeks), month (months), year (years), decade (decades), century (centuries), millennium (millennia) |
| Frequency | hertz (Hz) |
| Mass | gram (g), tonne, ton, grain (gr), dram (dr), ounce (oz), poundmass (lbm, lb, lbs), hundredweight (cwt), stick, stone |
| Electric Current | ampere (A) |
| Temperature | kelvin (K), celsius (degC), fahrenheit (degF), rankine (degR) |
| Amount of Substance | mole (mol) |
| Luminous Intensity | candela (cd) |
| Force | newton (N), dyne (dyn), poundforce (lbf), kip |
| Energy | joule (J), erg, Wh, BTU, electronvolt (eV) |
| Power | watt (W), hp |
| Pressure | Pa, psi, atm, torr, bar, mmHg, mmH2O, cmH2O |
| Electricity & Magnetism | ampere (A), coulomb (C), watt (W), volt (V), ohm, farad (F), weber (Wb), tesla (T), henry (H), siemens (S), electronvolt (eV) |
| Binary | bits (b), bytes (B) |
Note: All units support plural forms (e.g., 5 meters instead of 5 meter). Surface and volume units can be expressed as powers of length units (e.g., 100 in^2 instead of 100 sqin).
Unit Prefixes
Section titled “Unit Prefixes”Decimal Prefixes (Large):
| Name | Symbol | Value |
|---|---|---|
| deca | da | 10¹ |
| hecto | h | 10² |
| kilo | k | 10³ |
| mega | M | 10⁶ |
| giga | G | 10⁹ |
| tera | T | 10¹² |
| peta | P | 10¹⁵ |
| exa | E | 10¹⁸ |
| zetta | Z | 10²¹ |
| yotta | Y | 10²⁴ |
| ronna | R | 10²⁷ |
| quetta | Q | 10³⁰ |
Decimal Prefixes (Small):
| Name | Symbol | Value |
|---|---|---|
| deci | d | 10⁻¹ |
| centi | c | 10⁻² |
| milli | m | 10⁻³ |
| micro | u | 10⁻⁶ |
| nano | n | 10⁻⁹ |
| pico | p | 10⁻¹² |
| femto | f | 10⁻¹⁵ |
| atto | a | 10⁻¹⁸ |
| zepto | z | 10⁻²¹ |
| yocto | y | 10⁻²⁴ |
| ronto | r | 10⁻²⁷ |
| quecto | q | 10⁻³⁰ |
Binary Prefixes (for bits and bytes):
| Name | Symbol | Value |
|---|---|---|
| kibi | Ki | 1024 |
| mebi | Mi | 1024² |
| gibi | Gi | 1024³ |
| tebi | Ti | 1024⁴ |
| pebi | Pi | 1024⁵ |
| exi | Ei | 1024⁶ |
| zebi | Zi | 1024⁷ |
| yobi | Yi | 1024⁸ |
Examples:
5 km # Result: 5000 m100 GB # Result: 100 gigabytes2.5 GHz # Result: 2500000000 Hz1 KiB # Result: 1024 bytes (binary prefix)Important Notes on Units
Section titled “Important Notes on Units”Temperature Caution: Temperature scales like celsius and fahrenheit can behave unexpectedly in calculations because all operations work on the SI (Kelvin) representation. For reliable calculations, use kelvin (K) or rankine (degR):
14 degF * 2 # Result: 28 degF (270.93 K, not 526.3 K!)abs(-13 degF) # Result: -13 degF (248.15 K), not 13 degFComplex Unit Expressions: Use explicit operators and parentheses for clarity with complex units:
8.314 m^3 Pa / mol / K # Correct: 8.314 (m³ Pa) / (mol K)8.314 (m^3 * Pa) / (mol * K) # Explicit form (recommended)8.314 m^3 Pa / mol K # Wrong! Missing second '/' gives incorrect resultStrings
Section titled “Strings”Strings are enclosed by double quotes " or single quotes ':
Examples:
"hello" # Result: "hello"'hello' # Result: "hello"a = concat("hello", " world") # Result: "hello world"size(a) # Result: [11]
# String indexing and manipulationa[1:5] # Result: "hello"a[1] = "H" # Result: "H"a[7:12] = "there!" # Result: "there!"a # Result: "Hello there!"
# String conversionnumber("300") # Result: 300string(300) # Result: "300"Matrices
Section titled “Matrices”Matrices can be created using square brackets:
Examples:
[1, 2, 3] # Result: [1, 2, 3] (size [3])[[1, 2, 3], [4, 5, 6]] # Result: [[1, 2, 3], [4, 5, 6]] (size [2, 3])[1, 2, 3; 4, 5, 6] # Result: [[1, 2, 3], [4, 5, 6]] (size [2, 3])[[[1, 2], [3, 4]], [[5, 6], [7, 8]]] # Result: 3D matrix (size [2, 2, 2])
# Initialize matriceszeros(3, 2) # Result: [[0, 0], [0, 0], [0, 0]]ones(3) # Result: [1, 1, 1]5 * ones(2, 2) # Result: [[5, 5], [5, 5]]identity(2) # Result: [[1, 0], [0, 1]]1:4 # Result: [1, 2, 3, 4]0:2:10 # Result: [0, 2, 4, 6, 8, 10]Matrix indexing (one-based):
Examples:
a = [1, 2; 3, 4] # Result: [[1, 2], [3, 4]]a[1, 1] # Result: 1a[2, :] # Result: [3, 4]a[1:2, 2] # Result: [2, 4]
# Using 'end' keywordc = 5:9 # Result: [5, 6, 7, 8, 9]c[end - 1 : -1 : 2] # Result: [8, 7, 6] (reverse with step)
# Modifying matricesb = zeros(2, 2) # Result: [[0, 0], [0, 0]]b[1, 1:2] = [5, 6] # Result: [[5, 6], [0, 0]]b[2, :] = [7, 8] # Result: [[5, 6], [7, 8]]
# Matrix calculationsd = a * b # Result: [[19, 22], [43, 50]]d[2, 1] # Result: 43d[2, 1:end] # Result: [43, 50]Objects
Section titled “Objects”Objects are enclosed by curly brackets:
Examples:
{a: 2 + 1, b: 4} # Result: {a: 3, b: 4}{"a": 2 + 1, "b": 4} # Result: {a: 3, b: 4}{a: 2, b: {c: 3, d: 4}} # Result: {a: 2, b: {c: 3, d: 4}}
# Access propertiesobj = {prop: 42}; obj.prop # Result: 42obj = {prop: 42}; obj["prop"] # Result: 42
# Set properties (returns the whole object)obj = {a: 12}obj.prop = 43 # Result: {a: 12, prop: 43}obj["prop"] = 43 # Result: {a: 12, prop: 43}Implicit Multiplication
Section titled “Implicit Multiplication”Implicit multiplication allows natural notation without the * operator:
Examples:
2 pi # Result: 6.283185307179586(1+2)(3+4) # Result: 21Important notes:
- Implicit multiplication has higher precedence than explicit multiplication
- Division is evaluated before implicit multiplication in patterns like
20 / 4 kg
Examples:
| Expression | Interpreted As | Result |
|---|---|---|
(1 + 3) pi | (1 + 3) * pi | 12.566370614359172 |
(4 - 1) 2 | (4 - 1) * 2 | 6 |
3 / 4 mm | (3 / 4) * mm | 0.75 mm |
2 + 3 i | 2 + (3 * i) | 2 + 3i |
(1 + 2) (4 - 2) | (1 + 2) * (4 - 2) | 6 |
sqrt(4) (1 + 2) | sqrt(4) * (1 + 2) | 6 |
8 pi / 2 pi | (8 * pi) / (2 * pi) | 4 |
pi / 2 pi | pi / (2 * pi) | 0.5 |
1 / 2i | (1 / 2) * i | 0.5 i |
8.314 J / mol K | 8.314 J / (mol * K) | 8.314 J / (mol * K) |
20 kg / 4 kg | (20 kg) / (4 kg) | 5 |
20 / 4 kg | (20 / 4) kg | 5 kg |
Multi-line Expressions
Section titled “Multi-line Expressions”Expressions can span multiple lines using newline or semicolon ;:
Examples:
# Multiple statements (each on new line)1 * 32 * 33 * 3# Results: 3, 6, 9
# Semicolon hides outputa=3; b=4; a + ba * b# Results: 7, 12 (a=3 and b=4 are hidden)
# Expression spread over multiple linesa = 2 + 3# Result: 5Comments
Section titled “Comments”Comments start with # and end at the end of the line:
Examples:
# define some variableswidth = 3 # Result: 3height = 4 # Result: 4width * height # calculate the area # Result: 12What is an expression calculator?
An expression calculator is a mathematical tool that evaluates complex
mathematical expressions using proper mathematical notation. Unlike basic
calculators that only handle simple operations, an expression calculator can
process multi-step calculations, functions, variables, matrices, and units
in a single expression like 2 * pi * 5^2 + sqrt(16).
How do I calculate powers and exponents?
Use the ^ operator for exponentiation. For example: 2^3 gives 8, 5^2
gives 25. You can also use the pow() function: pow(2, 3) also gives 8.
For element-wise power on arrays, use .^ like [2,3] .^ [3,3] which gives
[8, 27].
Can I use variables in my calculations?
Yes! Define variables using the = operator. For example: a = 5, then b = 10, then a * b gives 50. Variables persist throughout your session, so
you can reference them in later calculations. Variable names must start with
a letter, underscore, or dollar sign.
How do I calculate square roots?
Use the sqrt() function. For example: sqrt(25) gives 5, sqrt(144)
gives 12. For negative numbers, it returns complex numbers: sqrt(-4) gives
2i. You can also use fractional exponents: 25^0.5 is equivalent to
sqrt(25).
What mathematical constants are available?
Common constants include: pi or PI (3.14159…), e or E
(2.71828…), tau (6.28318…), phi (golden ratio, 1.618…), i
(imaginary unit), Infinity, SQRT2 (1.414…), LN2, LN10, and many
more. Just type the constant name in your expression.
How do I use trigonometric functions?
Use functions like sin(), cos(), tan(), asin(), acos(), atan().
By default, they work in radians: sin(pi/2) gives 1. To use degrees, add
the deg unit: sin(90 deg) also gives 1. Inverse functions return
radians: asin(1) gives approximately 1.5708.
Can I convert between units?
Yes! Use the to or in keyword. Examples: 2 inch to cm gives 5.08 cm,
20 celsius to fahrenheit gives 68°F, 90 km/h to m/s gives 25 m/s. You
can also combine units in calculations: 5 kg * 2 m/s^2 gives 10 N
(force).
How do I create and use matrices?
Create matrices with square brackets: [1, 2, 3] for a row vector, [[1, 2], [3, 4]] for a 2x2 matrix. Use semicolons to separate rows: [1, 2; 3, 4]. Access elements with 1-based indexing: A[1, 2] gets the element at
row 1, column 2. Matrix operations include * for multiplication, inv()
for inverse, det() for determinant.
What is implicit multiplication?
Implicit multiplication allows you to omit the * operator for natural
mathematical notation. For example: 2 pi equals 2 * pi, (1+2)(3+4)
equals (1+2) * (3+4) giving 21. It has higher precedence than explicit
multiplication, so 8 pi / 2 pi equals (8*pi) / (2*pi) which is 4.
How do I define custom functions?
Define functions by assigning an expression to a function call. Examples:
f(x) = x^2 creates a squaring function, then f(5) gives 25.
Multi-parameter functions: g(x, y) = x^y, then g(2, 3) gives 8. You can
pass functions as parameters: twice(func, x) = func(func(x)).
Can I use parentheses to control calculation order?
Yes! Parentheses override the default operator precedence. For example: 2 + 3 * 4 gives 14 (multiplication first), but (2 + 3) * 4 gives 20 (addition
first). Use parentheses liberally to make your expressions clear and ensure
correct order of operations.
How do I calculate percentages?
Use the % operator: 8% gives 0.08. For percentage additions: 100 + 3%
gives 103. For percentage subtractions: 100 - 3% gives 97. For modulus
(remainder), use mod: 8 mod 3 gives 2, or the % operator in context:
8 % 3.
What are complex numbers and how do I use them?
Complex numbers use the imaginary unit i where i * i = -1. Create them
like 2 + 3i or 4 - i. Operations work naturally: (2 + 3i) + (4 - i)
gives 6 + 2i. Functions like sqrt(-4) return 2i. Extract parts with
re() and im(): re(2 + 3i) gives 2, im(2 + 3i) gives 3.
How do I use logarithms?
Use log() for natural logarithm (base e): log(e) gives 1. For other
bases, pass two arguments: log(100, 10) gives 2 (log base 10 of 100). Also
available: log10() for base-10 and log2() for base-2. The inverse is
exp(): exp(1) gives e.
Can I perform bitwise operations?
Yes! Use & (AND), | (OR), ^| (XOR), ~ (NOT), << (left
shift), >> (right shift), >>> (unsigned right shift).
Examples: 5 & 3 gives 1, 5 | 3 gives 7, 4 << 1 gives 8. These
work on integers and follow standard bitwise operation rules.
How do I use logical operators?
Logical operators include and, or, not, xor. Examples: true and false gives false, true or false gives true, not true gives false. They
use lazy evaluation: in false and x, x is not evaluated. Comparison
operators (<, >, ==, !=) can be chained: 5 < x < 10
means 5 < x and x < 10.
What statistical functions are available?
Statistical functions include: mean() for average, median() for middle
value, std() for standard deviation, variance() for variance, min()
and max() for extremes, sum() for total, prod() for product. Example:
mean([1, 2, 3, 4, 5]) gives 3.
How do I round numbers?
Use round() to round to nearest integer: round(3.7) gives 4. Use
floor() to round down: floor(3.7) gives 3. Use ceil() to round up:
ceil(3.2) gives 4. Use fix() to round towards zero: fix(-3.7) gives
-3. Add a second parameter for decimal places: round(3.14159, 2) gives
3.14.
Can I work with different number bases like binary or hexadecimal?
Yes! Prefix with 0b for binary: 0b11 gives 3. Use 0o for octal: 0o77
gives 63. Use 0x for hexadecimal: 0xff gives 255. Convert back with
format(): format(255, {notation: "hex"}) gives “0xff”.
These also support radix points: 0b1.1 gives 1.5.
How do I use the ternary conditional operator?
Use condition ? valueIfTrue : valueIfFalse. Examples: 15 > 100 ? 1 : -1 gives -1, x > 0 ? "positive" : "negative" returns different strings
based on x. This is useful for conditional calculations without defining
separate functions.
What are ranges and how do I create them?
Use the : operator to create ranges. 1:4 creates [1, 2, 3, 4]. Specify
step with three values: 0:2:10 creates [0, 2, 4, 6, 8, 10]. Negative
steps work too: 10:-2:0 creates [10, 8, 6, 4, 2, 0]. Ranges are useful
for loops and array creation.
How do I access matrix elements?
Use 1-based indexing with square brackets. For matrix A = [[1, 2], [3, 4]]: A[1, 1] gives 1, A[2, 2] gives 4. Use : for entire rows/columns:
A[1, :] gives [1, 2], A[:, 2] gives [2, 4]. Use ranges: A[1:2, 1]
gives [1, 3]. The end keyword refers to the last index.
Can I use physical constants in my calculations?
Yes! The calculator includes constants like speedOfLight (299792458 m/s),
gravitationConstant, planckConstant, avogadro (6.022e23), boltzmann,
electronMass, protonMass, and many more. Example: speedOfLight * 5 s
calculates distance light travels in 5 seconds.
How do I use the map function?
The map() function applies a function to each element of an array.
Examples: map([1, 2, 3, 4], sqrt) gives [1, 1.414, 1.732, 2]. You can
define inline functions: map([1, 2, 3, 4], f(x) = x^2) gives [1, 4, 9, 16]. Or use expressions: map([1, 2, 3, 4], x^2).
What’s the difference between BigNumber and regular numbers?
Regular numbers use JavaScript’s floating-point arithmetic, which can have
rounding errors: 0.1 + 0.2 gives 0.30000000000000004. BigNumbers provide
arbitrary precision: bignumber(0.1) + bignumber(0.2) gives exactly 0.3.
Use BigNumbers when exact decimal arithmetic is critical, like financial
calculations.
How do I multiply matrices?
Use * for matrix multiplication: [[1, 2], [3, 4]] * [[5, 6], [7, 8]]
performs matrix multiplication. For element-wise multiplication, use .*:
[1, 2, 3] .* [2, 3, 4] gives [2, 6, 12]. The dimensions must be
compatible: for A * B, columns of A must equal rows of B.
Can I use scientific notation?
Yes! Use e for scientific notation: 1.4e3 equals 1400, 22e-3 equals
0.022, 6.022e23 represents Avogadro’s number. You can perform calculations
directly: 1e6 * 2e3 gives 2e9 (2 billion). This is useful for very large
or very small numbers.
How do I calculate factorials?
Use the ! operator after a number: 5! gives 120, 10! gives 3628800.
Factorials only work on non-negative integers. For large factorials,
consider using the gamma() function: gamma(n+1) equals n! but also
works for non-integers.
What string operations are supported?
Create strings with quotes: "hello" or 'hello'. Concatenate with
concat(): concat("hello", " world"). Get length with size():
size("hello") gives [5]. Index strings: "hello"[1] gives “h”. Extract
substrings: "hello world"[1:5] gives “hello”. Convert with string() and
number().
How do I create identity matrices?
Use identity(n) or eye(n) to create an n×n identity matrix. Example:
identity(3) gives [[1, 0, 0], [0, 1, 0], [0, 0, 1]]. For non-square
identity matrices, use eye(m, n): eye(2, 3) gives [[1, 0, 0], [0, 1, 0]].
Can I write multi-line expressions?
Yes! Separate statements with newlines or semicolons. Example: a = 5; b = 10; a * b executes all three statements. Semicolons hide intermediate
results. You can also break long expressions across lines: result = 2 + 3 + 4 + 5 + 6 can be written as result = 2 + 3 + on one line and 4 + 5 + 6
on the next.
How do I add comments to my calculations?
Use # to start a comment that extends to the end of the line. Example:
width = 5 # in meters, height = 3 # in meters, area = width * height # calculate area. Comments are ignored during calculation and help document
your work.
What is the precedence of operators?
Operators are evaluated in order of precedence (highest to lowest):
parentheses/brackets, function calls, transpose, factorial, exponentiation,
unary operators, percentage, implicit multiplication, explicit
multiplication/division, addition/subtraction, ranges, comparisons, logical
operators, assignment. Use parentheses when in doubt: (2 + 3) * 4 vs 2 + 3 * 4.
How do I transpose a matrix?
Use the ' operator or transpose() function. Example: [[1, 2], [3, 4]]'
gives [[1, 3], [2, 4]]. This swaps rows and columns. For complex matrices,
' is the conjugate transpose. Use transpose() for non-conjugate
transpose of complex matrices.
Can I calculate matrix determinants?
Yes! Use det() function on square matrices. Example: det([[1, 2], [3, 4]]) gives -2. The determinant is only defined for square matrices. It’s
useful for checking if a matrix is invertible (non-zero determinant) and in
various linear algebra applications.
How do I invert a matrix?
Use the inv() function on square, non-singular matrices. Example:
inv([[1, 2], [3, 4]]) gives [[-2, 1], [1.5, -0.5]]. Verify: A * inv(A)
gives the identity matrix. If the determinant is zero, the matrix is not
invertible.
What is the nullish coalescing operator?
The ?? operator returns the right operand when the left is null or
undefined, otherwise returns the left operand. Example: null ?? 2 gives
2, 5 ?? 2 gives 5, undefined ?? "default" gives “default”. This is
useful for providing default values.
How do I use optional chaining?
The ?. operator safely accesses nested object properties. If the left side
is null or undefined, it returns undefined instead of throwing an
error. Example: obj?.prop?.nested safely accesses deeply nested properties
even if intermediate properties don’t exist.
Can I create 3D or higher-dimensional matrices?
Yes! Nest arrays to create multi-dimensional matrices. Example: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] creates a 2×2×2 3D matrix. Access elements with
multiple indices: A[1, 1, 2]. Many operations work on n-dimensional
arrays.
How do I calculate absolute values?
Use the abs() function. Example: abs(-5) gives 5, abs(3) gives 3. For
complex numbers, it returns the magnitude: abs(3 + 4i) gives 5 (since
sqrt(3² + 4²) = 5). This works with matrices too, applying element-wise.
What hyperbolic functions are available?
Hyperbolic functions include sinh(), cosh(), tanh(), asinh(),
acosh(), atanh(). Example: sinh(0) gives 0, cosh(0) gives 1. These
are useful in calculus, physics, and engineering applications involving
hyperbolas and exponential relationships.
How do I create zero and one matrices?
Use zeros(m, n) for an m×n matrix of zeros: zeros(2, 3) gives [[0, 0, 0], [0, 0, 0]]. Use ones(m, n) for ones: ones(2, 2) gives [[1, 1], [1, 1]]. Single parameter creates square matrices or vectors: zeros(3) gives
[0, 0, 0].
Can I calculate dot products and cross products?
Yes! Use dot() for dot product: dot([1, 2, 3], [4, 5, 6]) gives 32. Use
cross() for cross product of 3D vectors: cross([1, 0, 0], [0, 1, 0])
gives [0, 0, 1]. These are essential operations in vector mathematics and
physics.
How do I handle infinity and NaN?
Use Infinity for positive infinity, -Infinity for negative. NaN
represents “Not a Number”. Examples: 1 / 0 gives Infinity, 0 / 0 gives
NaN, log(-1) gives NaN. Check with isNaN() and isFinite():
isNaN(0/0) gives true, isFinite(1/0) gives false.
What’s the difference between = and ==?
= is the assignment operator: x = 5 assigns 5 to variable x. == is the
equality comparison operator: 5 == 5 gives true, 5 == 3 gives false. Use
!= for inequality: 5 != 3 gives true. Always use == when comparing
values, not =.
How do I calculate GCD and LCM?
Use gcd() for Greatest Common Divisor: gcd(12, 18) gives 6. Use lcm()
for Least Common Multiple: lcm(12, 18) gives 36. These work with multiple
arguments: gcd(12, 18, 24) gives 6. Useful for fraction simplification and
number theory.
Can I use random numbers?
Yes! Use random() for a random number between 0 and 1: random() gives
something like 0.7392. Use randomInt(max) or randomInt(min, max) for
integers: randomInt(1, 6) simulates a die roll. Use pickRandom(array) to
select a random element from an array.
How do I check if a number is prime?
While there’s no built-in isPrime() function in the basic calculator, you
can create a custom function: isPrime(n) = n > 1 and sum(map(1:n, x -> n mod x == 0)) == 2. This counts divisors - a prime has exactly 2
divisors (1 and itself).