2019-03-31 17:05:09 +08:00
.. _cmd-math:
2020-03-31 23:37:38 +08:00
math - perform mathematics calculations
2019-01-03 12:10:47 +08:00
=======================================
2018-12-17 09:39:33 +08:00
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
2022-01-09 17:25:55 +08:00
`` math `` [(**-s** | **--scale** ) *N* ] [(**-b** | **--base** ) *BASE* ] *EXPRESSION* ...
2018-12-18 09:58:24 +08:00
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Description
2019-01-03 12:10:47 +08:00
-----------
2018-12-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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 23:21:28 +08:00
2018-12-17 05:08:41 +08:00
The following options are available:
2021-12-25 08:39:47 +08: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-08 01:03:19 +08:00
2018-12-19 10:44:30 +08:00
Return Values
2019-01-03 12:10:47 +08:00
-------------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08: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-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Syntax
2019-01-03 12:10:47 +08:00
------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math `` knows some operators, constants, functions and can (obviously) read numbers.
2018-12-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Operators
2019-01-03 12:10:47 +08:00
---------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math `` knows the following operators:
2018-12-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
They are all used in an infix manner - `` 5 + 2 `` , not `` + 5 2 `` .
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Constants
2019-01-03 12:10:47 +08:00
---------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math `` knows the following constants:
2018-12-17 05:08:41 +08:00
2021-12-25 08:39:47 +08: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-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
Use them without a leading `` $ `` - `` pi - 3 `` should be about 0.
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Functions
2019-01-03 12:10:47 +08:00
---------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math `` supports the following functions:
2021-12-25 08:39:47 +08:00
`` abs ``
the absolute value, with positive sign
`` acos ``
2022-01-09 19:26:18 +08:00
arc cosine
2021-12-25 08:39:47 +08:00
`` asin ``
2022-01-09 19:26:18 +08:00
arc sine
2021-12-25 08:39:47 +08:00
`` atan ``
2022-01-09 19:26:18 +08:00
arc tangent
2021-12-25 08:39:47 +08:00
`` atan2 ``
2022-01-09 19:26:18 +08:00
arc tangent of two variables
2021-12-25 08:39:47 +08: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 19:26:18 +08:00
round number up to nearest integer
2021-12-25 08:39:47 +08:00
`` cos ``
2022-01-09 19:26:18 +08:00
the cosine
2021-12-25 08:39:47 +08:00
`` cosh ``
2022-01-09 19:26:18 +08:00
hyperbolic cosine
2021-12-25 08:39:47 +08:00
`` exp ``
2022-01-09 19:26:18 +08:00
the base-e exponential function
2021-12-25 08:39:47 +08:00
`` fac ``
2022-01-09 19:26:18 +08:00
factorial - also known as `` x! `` (`` x * (x - 1) * (x - 2) * ... * 1 `` )
2021-12-25 08:39:47 +08:00
`` floor ``
2022-01-09 19:26:18 +08:00
round number down to nearest integer
2021-12-25 08:39:47 +08:00
`` ln ``
2022-01-09 19:26:18 +08:00
the base-e logarithm
2021-12-25 08:39:47 +08:00
`` log `` or `` log10 ``
2022-01-09 19:26:18 +08:00
the base-10 logarithm
2021-12-25 08:39:47 +08:00
`` log2 ``
2022-01-09 19:26:18 +08:00
the base-2 logarithm
2021-12-25 08:39:47 +08:00
`` max ``
2022-01-09 19:26:18 +08:00
returns the larger of two numbers
2021-12-25 08:39:47 +08:00
`` min ``
2022-01-09 19:26:18 +08:00
returns the smaller of two numbers
2021-12-25 08:39:47 +08:00
`` ncr ``
2022-01-09 19:26:18 +08: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 08:39:47 +08:00
`` npr ``
2022-01-09 19:26:18 +08:00
the number of subsets of size r that can be taken from a set of n elements (including different order)
2021-12-25 08:39:47 +08:00
`` pow(x,y) ``
returns x to the y (and can be written as `` x ^ y `` )
`` round ``
2022-01-09 19:26:18 +08:00
rounds to the nearest integer, away from 0
2021-12-25 08:39:47 +08:00
`` sin ``
2022-01-09 19:26:18 +08:00
the sine function
2021-12-25 08:39:47 +08:00
`` sinh ``
2022-01-09 19:26:18 +08:00
the hyperbolic sine
2021-12-25 08:39:47 +08:00
`` sqrt ``
2022-01-09 19:26:18 +08:00
the square root - (can also be written as `` x ^ 0.5 `` )
2021-12-25 08:39:47 +08:00
`` tan ``
2022-01-09 19:26:18 +08:00
the tangent
2021-12-25 08:39:47 +08:00
`` tanh ``
2022-01-09 19:26:18 +08:00
the hyperbolic tangent
2018-12-17 05:08:41 +08:00
2021-03-12 02:46:02 +08:00
All of the trigonometric functions use radians (the pi-based scale, not 360°).
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Examples
2019-01-03 12:10:47 +08:00
--------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math 1+1 `` outputs 2.
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math $status - 128 `` outputs the numerical exit status of the last command minus 128.
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math 10 / 6 `` outputs `` 1.666667 `` .
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math -s0 10.0 / 6.0 `` outputs `` 1 `` .
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math -s3 10 / 6 `` outputs `` 1.666 `` .
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
`` math "sin(pi)" `` outputs `` 0 `` .
2018-12-17 05:08:41 +08:00
2019-02-25 07:01:16 +08:00
`` math 5 \* 2 `` or `` math "5 * 2" `` or `` math 5 "*" 2 `` all output `` 10 `` .
2019-05-30 22:00:45 +08:00
`` math 0xFF `` outputs 255, `` math 0 x 3 `` outputs 0 (because it computes 0 multiplied by 3).
2021-03-30 23:21:28 +08:00
`` math bitand 0xFE, 0x2e `` outputs 46.
2020-08-25 22:45:31 +08:00
`` math "bitor(9,2)" `` outputs 11.
2020-10-08 01:03:19 +08:00
`` math --base=hex 192 `` prints `` 0xc0 `` .
2021-03-12 02:46:02 +08:00
`` math 'ncr(49,6)' `` prints 13983816 - that's the number of possible picks in 6-from-49 lotto.
2018-12-19 10:44:30 +08:00
Compatibility notes
2019-01-03 12:10:47 +08:00
-------------------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08: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-17 05:08:41 +08:00
2021-02-21 21:34:15 +08: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.