2019-03-31 09:05:09 +00:00
.. _cmd-math:
2020-03-31 15:37:38 +00:00
math - perform mathematics calculations
2019-01-03 04:10:47 +00:00
=======================================
2018-12-17 01:39:33 +00:00
2018-12-18 01:58:24 +00:00
Synopsis
--------
2018-12-16 21:08:41 +00:00
2022-01-09 09:25:55 +00:00
`` math `` [(**-s** | **--scale** ) *N* ] [(**-b** | **--base** ) *BASE* ] *EXPRESSION* ...
2018-12-18 01:58:24 +00:00
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Description
2019-01-03 04:10:47 +00:00
-----------
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
`` math `` performs mathematical calculations.
It supports simple operations such as addition, subtraction, and so on, as well as functions like `` abs() `` , `` sqrt() `` and `` ln() `` .
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
By default, the output is a floating-point number with trailing zeroes trimmed.
To get a fixed representation, the `` --scale `` option can be used, including `` --scale=0 `` for integer output.
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
Keep in mind that parameter expansion happens before expressions are evaluated.
This can be very useful in order to perform calculations involving shell variables or the output of command substitutions, but it also means that parenthesis (`` () `` ) and the asterisk (`` * `` ) glob character have to be escaped or quoted.
`` x `` can also be used to denote multiplication, but it needs to be followed by whitespace to distinguish it from hexadecimal numbers.
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
Parentheses for functions are optional - `` math sin pi `` prints `` 0 `` .
However, a comma will bind to the inner function, so `` math pow sin 3, 5 `` is an error because it tries to give `` sin `` the arguments `` 3 `` and `` 5 `` .
When in doubt, use parentheses.
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
`` math `` ignores whitespace between arguments and takes its input as multiple arguments (internally joined with a space), so `` math 2 +2 `` and `` math "2 + 2" `` work the same.
`` math 2 2 `` is an error.
2021-03-30 15:21:28 +00:00
2018-12-16 21:08:41 +00:00
The following options are available:
2021-12-25 00:39:47 +00:00
**-s** *N* or **--scale** *N*
Sets the scale of the result.
`` N `` must be an integer or the word "max" for the maximum scale.
A scale of zero causes results to be rounded down to the nearest integer.
So `` 3/2 `` returns `` 1 `` rather than `` 2 `` which `` 1.5 `` would normally round to.
This is for compatibility with `` bc `` which was the basis for this command prior to fish 3.0.0.
Scale values greater than zero causes the result to be rounded using the usual rules to the specified number of decimal places.
**-b** *BASE* or **--base** *BASE*
Sets the numeric base used for output (`` math `` always understands hexadecimal numbers as input).
It currently understands "hex" or "16" for hexadecimal and "octal" or "8" for octal and implies a scale of 0 (other scales cause an error), so it will truncate the result down to an integer.
This might change in the future.
Hex numbers will be printed with a `` 0x `` prefix.
Octal numbers will have a prefix of `` 0 `` but aren't understood by `` math `` as input.
2020-10-07 17:03:19 +00:00
2018-12-19 02:44:30 +00:00
Return Values
2019-01-03 04:10:47 +00:00
-------------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
If the expression is successfully evaluated and doesn't over/underflow or return NaN the return `` status `` is zero (success) else one.
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Syntax
2019-01-03 04:10:47 +00:00
------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math `` knows some operators, constants, functions and can (obviously) read numbers.
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
For numbers, `` . `` is always the radix character regardless of locale - `` 2.5 `` , not `` 2,5 `` .
Scientific notation (`` 10e5 `` ) and hexadecimal (`` 0xFF `` ) are also available.
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Operators
2019-01-03 04:10:47 +00:00
---------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math `` knows the following operators:
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
`` + ``
for addition
`` - ``
for subtraction
`` * `` or `` x ``
for multiplication
`` / ``
for division
(Note that `` * `` is the glob character and needs to be quoted or escaped, `` x `` needs to be followed by whitespace or it looks like `` 0x `` hexadecimal notation.)
`` ^ ``
for exponentiation
`` % ``
for modulo
`` ( `` or `` ) ``
for grouping.
(These need to be quoted or escaped because `` () `` denotes a command substitution.)
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
They are all used in an infix manner - `` 5 + 2 `` , not `` + 5 2 `` .
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Constants
2019-01-03 04:10:47 +00:00
---------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math `` knows the following constants:
2018-12-16 21:08:41 +00:00
2021-12-25 00:39:47 +00:00
`` e ``
Euler's number
`` pi ``
π, you know this one.
Half of Tau
`` tau ``
Equivalent to 2π, or the number of radians in a circle
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
Use them without a leading `` $ `` - `` pi - 3 `` should be about 0.
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Functions
2019-01-03 04:10:47 +00:00
---------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math `` supports the following functions:
2021-12-25 00:39:47 +00:00
`` abs ``
the absolute value, with positive sign
`` acos ``
2022-01-09 11:26:18 +00:00
arc cosine
2021-12-25 00:39:47 +00:00
`` asin ``
2022-01-09 11:26:18 +00:00
arc sine
2021-12-25 00:39:47 +00:00
`` atan ``
2022-01-09 11:26:18 +00:00
arc tangent
2021-12-25 00:39:47 +00:00
`` atan2 ``
2022-01-09 11:26:18 +00:00
arc tangent of two variables
2021-12-25 00:39:47 +00:00
`` bitand `` , `` bitor `` and `` bitxor ``
perform bitwise operations.
These will throw away any non-integer parts andd interpret the rest as an int.
`` ceil ``
2022-01-09 11:26:18 +00:00
round number up to nearest integer
2021-12-25 00:39:47 +00:00
`` cos ``
2022-01-09 11:26:18 +00:00
the cosine
2021-12-25 00:39:47 +00:00
`` cosh ``
2022-01-09 11:26:18 +00:00
hyperbolic cosine
2021-12-25 00:39:47 +00:00
`` exp ``
2022-01-09 11:26:18 +00:00
the base-e exponential function
2021-12-25 00:39:47 +00:00
`` fac ``
2022-01-09 11:26:18 +00:00
factorial - also known as `` x! `` (`` x * (x - 1) * (x - 2) * ... * 1 `` )
2021-12-25 00:39:47 +00:00
`` floor ``
2022-01-09 11:26:18 +00:00
round number down to nearest integer
2021-12-25 00:39:47 +00:00
`` ln ``
2022-01-09 11:26:18 +00:00
the base-e logarithm
2021-12-25 00:39:47 +00:00
`` log `` or `` log10 ``
2022-01-09 11:26:18 +00:00
the base-10 logarithm
2021-12-25 00:39:47 +00:00
`` log2 ``
2022-01-09 11:26:18 +00:00
the base-2 logarithm
2021-12-25 00:39:47 +00:00
`` max ``
2022-01-09 11:26:18 +00:00
returns the larger of two numbers
2021-12-25 00:39:47 +00:00
`` min ``
2022-01-09 11:26:18 +00:00
returns the smaller of two numbers
2021-12-25 00:39:47 +00:00
`` ncr ``
2022-01-09 11:26:18 +00:00
"from n choose r" combination function - how many subsets of size r can be taken from n (order doesn't matter)
2021-12-25 00:39:47 +00:00
`` npr ``
2022-01-09 11:26:18 +00:00
the number of subsets of size r that can be taken from a set of n elements (including different order)
2021-12-25 00:39:47 +00:00
`` pow(x,y) ``
returns x to the y (and can be written as `` x ^ y `` )
`` round ``
2022-01-09 11:26:18 +00:00
rounds to the nearest integer, away from 0
2021-12-25 00:39:47 +00:00
`` sin ``
2022-01-09 11:26:18 +00:00
the sine function
2021-12-25 00:39:47 +00:00
`` sinh ``
2022-01-09 11:26:18 +00:00
the hyperbolic sine
2021-12-25 00:39:47 +00:00
`` sqrt ``
2022-01-09 11:26:18 +00:00
the square root - (can also be written as `` x ^ 0.5 `` )
2021-12-25 00:39:47 +00:00
`` tan ``
2022-01-09 11:26:18 +00:00
the tangent
2021-12-25 00:39:47 +00:00
`` tanh ``
2022-01-09 11:26:18 +00:00
the hyperbolic tangent
2018-12-16 21:08:41 +00:00
2021-03-11 18:46:02 +00:00
All of the trigonometric functions use radians (the pi-based scale, not 360°).
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Examples
2019-01-03 04:10:47 +00:00
--------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math 1+1 `` outputs 2.
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math $status - 128 `` outputs the numerical exit status of the last command minus 128.
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math 10 / 6 `` outputs `` 1.666667 `` .
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math -s0 10.0 / 6.0 `` outputs `` 1 `` .
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math -s3 10 / 6 `` outputs `` 1.666 `` .
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` math "sin(pi)" `` outputs `` 0 `` .
2018-12-16 21:08:41 +00:00
2019-02-24 23:01:16 +00:00
`` math 5 \* 2 `` or `` math "5 * 2" `` or `` math 5 "*" 2 `` all output `` 10 `` .
2019-05-30 14:00:45 +00:00
`` math 0xFF `` outputs 255, `` math 0 x 3 `` outputs 0 (because it computes 0 multiplied by 3).
2021-03-30 15:21:28 +00:00
`` math bitand 0xFE, 0x2e `` outputs 46.
2020-08-25 14:45:31 +00:00
`` math "bitor(9,2)" `` outputs 11.
2020-10-07 17:03:19 +00:00
`` math --base=hex 192 `` prints `` 0xc0 `` .
2021-03-11 18:46:02 +00:00
`` math 'ncr(49,6)' `` prints 13983816 - that's the number of possible picks in 6-from-49 lotto.
2018-12-19 02:44:30 +00:00
Compatibility notes
2019-01-03 04:10:47 +00:00
-------------------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
Fish 1.x and 2.x releases relied on the `` bc `` command for handling `` math `` expressions. Starting with fish 3.0.0 fish uses the tinyexpr library and evaluates the expression without the involvement of any external commands.
2018-12-16 21:08:41 +00:00
2021-02-21 13:34:15 +00:00
You don't need to use `` -- `` before the expression, even if it begins with a minus sign which might otherwise be interpreted as an invalid option. If you do insert `` -- `` before the expression, it will cause option scanning to stop just like for every other command and it won't be part of the expression.