Math.js

App · Utilities · 7 actions

Do the maths a workflow cannot: work out any calculation and get a real number back, convert units, summarise a column of figures, apply one formula to a whole list, re-base a number, and simplify or differentiate a formula. Math.js is the calculation engine behind mathjs.org, and its web service is free and needs no account or API key.

Actions

ActionWhat it does
Apply a formula to a list of valuesRun one formula over a whole list of numbers and get a list of answers back, in the same order.
Convert a number between basesTake a whole number and get it in all four bases at once - decimal, binary, octal and hexadecimal - each with and without its 0b/0o/0x prefix, so it drops straight into code or into a message.
Convert unitsConvert an amount from one unit into another - kilograms to pounds, Celsius to Fahrenheit, gigabytes to mebibytes, kilometres per hour to miles per hour.
Differentiate a formulaWork out the derivative of a formula with respect to one of its variables - the rate at which it changes.
Evaluate a calculationWork out a calculation and get the answer back as a real number, not text.
Get statistics for a list of numbersSummarise a column of numbers in one step: how many there are, their total, smallest, largest and range, the mean, median and most common value, the standard deviation and variance, and the quartiles.
Simplify a formulaReduce a formula to its simplest equivalent form without working out a number: 2x + 3x becomes 5 * x, and x * 0 + 1 becomes 1.

Apply a formula to a list of values

Run one formula over a whole list of numbers and get a list of answers back, in the same order. This is the step for a column of figures: add VAT to every price, convert every reading, round every total. Write the formula once using x for each value. It is worked out in a single request however long the list, so it costs the same as one calculation. Every value has to be a number - one that is not stops the step and says which, because Math.js works the list out in one go and cannot skip an entry. No API key required.

Inputs

ValuesvalueslistRequired
The numbers to run the formula over. Wire in a list from an earlier step, or type them one at a time.
FormulaformulatextRequired
The formula to apply, written once, with x standing for each value in turn - x * 1.2 adds 20%.
Significant digitsprecisionnumber
Optional. How many significant digits the answer is worked out to. The default of 14 hides the tiny rounding errors computers make, so 0.1 + 0.2 comes back as 0.3 rather than 0.30000000000000004. Raise it for scientific work that needs the full figure, or lower it for a rough number. This is significant digits, not decimal places - to round to two decimals, use round(value, 2) in the calculation itself.From 1 to 64. Default: 14.

Outputs

answerslist
The same answers as text, which is what to use when they carry units.
countnumber
How many values were worked out.
formulatext
The formula that was applied.
precisionnumber
The significant digits the answers were worked out to.
resultslist
One row per value, in the order they were given: the value that went in and the answer that came out.
valueslist
Just the answers, as numbers, in the same order as the values that went in. This is the field to wire into another step that wants a list of numbers.
Example output
{
  "answers": [
    "23.99",
    "6"
  ],
  "count": 2,
  "formula": "round(x * 1.2, 2)",
  "precision": 14,
  "results": [
    {
      "answer": "23.99",
      "input": 19.99,
      "items": null,
      "type": "number",
      "unit": null,
      "value": 23.99
    },
    {
      "answer": "6",
      "input": 5,
      "items": null,
      "type": "number",
      "unit": null,
      "value": 6
    }
  ],
  "values": [
    23.99,
    6
  ]
}

Convert a number between bases

Take a whole number and get it in all four bases at once - decimal, binary, octal and hexadecimal - each with and without its 0b/0o/0x prefix, so it drops straight into code or into a message. A number pasted in with a prefix is read as that base whatever the field says, so 0xFF works without changing anything. Hexadecimal comes back in upper case, the way it is written. No API key required.

Inputs

NumbervaluetextRequired
The whole number to convert. Digit separators - spaces, commas or underscores - are ignored, so 1_0110_0111 is fine.
The number is written infrom_basetext
Optional. Which base the number is written in. Decimal unless you say otherwise - and a 0b, 0o or 0x prefix on the number itself wins over this.One of: decimal (Decimal (0-9)), binary (Binary (0-1)), octal (Octal (0-7)), hexadecimal (Hexadecimal (0-9 and A-F)). Default: decimal.

Outputs

binarytext
The number in base 2, prefixed ("0b11111111").
binary_digitstext
The binary digits without the prefix ("11111111").
decimaltext
The number in base 10 ("255").
hexadecimaltext
The number in base 16, prefixed and upper case ("0xFF").
hexadecimal_digitstext
The hexadecimal digits without the prefix ("FF") - the form a colour code or a byte is written in.
octaltext
The number in base 8, prefixed ("0o377").
octal_digitstext
The octal digits without the prefix ("377").
read_astext
Which base the number was read as: decimal, binary, octal or hexadecimal. Worth checking when a prefix overrode the field.
read_as_nametext
The same thing in words ("Hexadecimal").
valuenumber
The number itself, as a number you can calculate with.
Example output
{
  "binary": "0b11111111",
  "binary_digits": "11111111",
  "decimal": "255",
  "hexadecimal": "0xFF",
  "hexadecimal_digits": "FF",
  "octal": "0o377",
  "octal_digits": "377",
  "read_as": "decimal",
  "read_as_name": "Decimal",
  "value": 255
}

Convert units

Convert an amount from one unit into another - kilograms to pounds, Celsius to Fahrenheit, gigabytes to mebibytes, kilometres per hour to miles per hour. Pick both units from the list; there is no syntax to get right, and two units that measure different things are refused before the request rather than coming back as an error. Temperatures are converted properly, offset and all, so 20 Celsius is 68 Fahrenheit rather than 36. The answer comes back as a number and as a sentence you can paste into a message. No API key required.

Inputs

AmountvaluenumberRequired
The amount to convert.
Fromfrom_unittextRequired
The unit the amount is in now.One of: mm (Millimetres (mm)), cm (Centimetres (cm)), m (Metres (m)), km (Kilometres (km)), um (Micrometres (um)), nm (Nanometres (nm)), inch (Inches (inch)), ft (Feet (ft)), yd (Yards (yd)), mi (Miles (mi)), and 92 more.
Toto_unittextRequired
The unit to convert it into. It has to measure the same kind of thing - a length into a length, a mass into a mass.One of: mm (Millimetres (mm)), cm (Centimetres (cm)), m (Metres (m)), km (Kilometres (km)), um (Micrometres (um)), nm (Nanometres (nm)), inch (Inches (inch)), ft (Feet (ft)), yd (Yards (yd)), mi (Miles (mi)), and 92 more.
Significant digitsprecisionnumber
Optional. How many significant digits the answer is worked out to. The default of 14 hides the tiny rounding errors computers make, so 0.1 + 0.2 comes back as 0.3 rather than 0.30000000000000004. Raise it for scientific work that needs the full figure, or lower it for a rough number. This is significant digits, not decimal places - to round to two decimals, use round(value, 2) in the calculation itself.From 1 to 64. Default: 14.

Outputs

answertext
The converted amount with its unit ("68 degF").
from_unittext
The unit it started in, as Math.js spells it.
from_unit_nametext
That unit in words.
from_valuenumber
The amount that was converted.
measurestext
What both units measure - "length", "mass", "temperature" and so on.
precisionnumber
The significant digits the answer was worked out to.
summarytext
The whole conversion as a sentence - "20 Celsius = 68 Fahrenheit" - ready to put in a message.
unittext
The unit it was converted into, as Math.js spells it ("degF").
unit_nametext
That unit in words ("Fahrenheit").
valuenumber
The converted amount, as a number on its own - the field to use in another calculation.
Example output
{
  "answer": "68 degF",
  "from_unit": "degC",
  "from_unit_name": "Celsius",
  "from_value": 20,
  "measures": "temperature",
  "precision": 14,
  "summary": "20 Celsius = 68 Fahrenheit",
  "unit": "degF",
  "unit_name": "Fahrenheit",
  "value": 68
}

Differentiate a formula

Work out the derivative of a formula with respect to one of its variables - the rate at which it changes. x^2 + 2x differentiated by x is 2 * (x + 1). The answer is simplified on the way out, and any other letters in the formula are held constant. To then get a number from it, put the answer into Evaluate a calculation with the variable as a named value. No API key required.

Inputs

FormulaformulatextRequired
The formula to differentiate, on one line.
With respect tovariabletext
Optional. Which variable to differentiate by. x unless you say otherwise. Every other letter in the formula is treated as a constant.Default: x.

Outputs

derivativetext
The derivative, simplified.
formulatext
The formula that was given, as it was written.
variabletext
The variable it was differentiated by.
Example output
{
  "derivative": "2 * (x + 1)",
  "formula": "x^2 + 2x",
  "variable": "x"
}

Evaluate a calculation

Work out a calculation and get the answer back as a real number, not text. Write one step per line, exactly as you would on paper: a value you name on one line is there on the next, and the last line is the answer. Handles the things a workflow usually cannot - units (5.08 cm in inch), percentages, trigonometry, matrices, fractions and very large or very precise numbers. Values from earlier steps go in the named values field rather than into the text, so an empty one is reported rather than quietly breaking the sum. No API key required.

Inputs

CalculationexpressiontextRequired
What to work out, one step per line. The last line is the answer, and every line's own answer comes back too. Anything after a # is a note to yourself.
Named valuesvaluesobject
Optional. Values from earlier steps, given names the calculation can use - price 19.99, quantity 3. This is the safe way to bring a value in: pasted into the text instead, an empty value breaks the calculation and a negative one silently turns into a subtraction.
Significant digitsprecisionnumber
Optional. How many significant digits the answer is worked out to. The default of 14 hides the tiny rounding errors computers make, so 0.1 + 0.2 comes back as 0.3 rather than 0.30000000000000004. Raise it for scientific work that needs the full figure, or lower it for a rough number. This is significant digits, not decimal places - to round to two decimals, use round(value, 2) in the calculation itself.From 1 to 64. Default: 14.

Outputs

answertext
The answer, ready to put in a message. Text answers have the quotation marks Math.js prints around them taken off, and a compound unit is closed up ("62.14 mi/h", not "62.14 mi / h").
itemslist
The entries, when the answer is a list or a matrix - numbers where they are numbers. Empty for every other kind of answer.
precisionnumber
The significant digits the answer was worked out to.
step_countnumber
How many lines were worked out. Blank lines and notes are not counted.
stepslist
Every line and what it came to, in order. Read this to show the working, or to pull out an intermediate figure such as a subtotal.
typetext
What kind of answer this is, and therefore which of the fields beside it to read: "number" (read value), "measurement" (a number with a unit - read value and unit), "list" (read items), "text", "boolean" (the answer is "true" or "false"), "expression" (something algebraic that Math.js could not reduce to a value, such as 5 * x or the complex number 3 + 2i) or "nothing".
unittext
The unit the answer is in, such as "inch" or "kg", or empty when it has none.
valuenumber
The answer as a number you can add up, compare or chart - the amount on its own when the answer carries a unit. Empty when the answer is not a number at all, and for Infinity and NaN, which have no numeric form a workflow can carry; the answer field has the word in those two cases.
Example output
{
  "answer": "2 inch",
  "items": null,
  "precision": 14,
  "step_count": 1,
  "steps": [
    {
      "answer": "2 inch",
      "expression": "5.08 cm in inch",
      "items": null,
      "line": 1,
      "type": "measurement",
      "unit": "inch",
      "value": 2
    }
  ],
  "type": "measurement",
  "unit": "inch",
  "value": 2
}

Get statistics for a list of numbers

Summarise a column of numbers in one step: how many there are, their total, smallest, largest and range, the mean, median and most common value, the standard deviation and variance, and the quartiles. Everything is worked out in a single request, so the whole summary costs what one average would. Both the sample and the population standard deviation come back, because which one is meant is the quietest way a figure like this goes wrong. No API key required.

Inputs

NumbersvalueslistRequired
The numbers to summarise. Wire in a list from an earlier step, or type them one at a time.
Significant digitsprecisionnumber
Optional. How many significant digits the answer is worked out to. The default of 14 hides the tiny rounding errors computers make, so 0.1 + 0.2 comes back as 0.3 rather than 0.30000000000000004. Raise it for scientific work that needs the full figure, or lower it for a rough number. This is significant digits, not decimal places - to round to two decimals, use round(value, 2) in the calculation itself.From 1 to 64. Default: 14.

Outputs

countnumber
How many numbers there were.
interquartile_rangenumber
The distance between the quartiles - the spread of the middle half, ignoring outliers.
lower_quartilenumber
The value a quarter of the way through, in order.
maximumnumber
The largest of them.
meannumber
The average - the total divided by the count.
mediannumber
The middle value once they are in order. Worth preferring to the mean when a few extreme values would drag it.
minimumnumber
The smallest of them.
modelist
The most common value. It is a list because several values can tie - and when every number appears once, every number is listed, which is the honest answer that there is no mode.
population_standard_deviationnumber
The same spread, treating these numbers as the whole population rather than a sample of one. Slightly smaller, and the right one when the list is genuinely everything.
population_variancenumber
The population variance.
precisionnumber
The significant digits the figures were worked out to.
rangenumber
The distance from smallest to largest.
standard_deviationnumber
How spread out they are, treating them as a sample of something larger. This is what a spreadsheet's STDEV gives.
sumnumber
Their total.
summarytext
The headline figures as a sentence, ready to put in a message.
upper_quartilenumber
The value three quarters of the way through.
valueslist
The numbers as they were read, in the order given.
variancenumber
The sample variance - the standard deviation squared.
Example output
{
  "count": 6,
  "interquartile_range": 3.75,
  "lower_quartile": 3.25,
  "maximum": 9,
  "mean": 5.3333333333333,
  "median": 5.5,
  "minimum": 2,
  "mode": [
    7
  ],
  "population_standard_deviation": 2.4944382578493,
  "population_variance": 6.2222222222222,
  "precision": 14,
  "range": 7,
  "standard_deviation": 2.7325202042559,
  "sum": 32,
  "summary": "6 numbers, from 2 to 9. Mean 5.3333333333333, median 5.5, standard deviation 2.7325202042559.",
  "upper_quartile": 7,
  "values": [
    3,
    7,
    7,
    2,
    9,
    4
  ],
  "variance": 7.4666666666667
}

Simplify a formula

Reduce a formula to its simplest equivalent form without working out a number: 2x + 3x becomes 5 * x, and x * 0 + 1 becomes 1. Use it to tidy a formula somebody has typed before showing it back to them, or to collapse one a workflow has built up piece by piece. It says whether anything actually changed. No API key required.

Inputs

FormulaformulatextRequired
The formula to simplify, on one line. It can hold any letters you like as variables.

Outputs

changedtrue/false
Whether simplifying made any difference. False means the formula was already as simple as it gets.
formulatext
The formula that was given, as it was written.
simplifiedtext
The simplest equivalent form Math.js could find.
Example output
{
  "changed": true,
  "formula": "2x + 3x",
  "simplified": "5 * x"
}

On this page