[math] Fix docs

We no longer use muparser, but tinyexpr.

tinyexpr does not have:

- "Statistical functions" like min, max, avg

- Multiple expressions separated with ","

[ci skip]
This commit is contained in:
Fabian Homborg 2018-02-11 14:00:01 +01:00
parent b75c3b968c
commit 234ebb5d7b

View File

@ -7,14 +7,10 @@ math [-sN | --scale=N] [--] EXPRESSION
\subsection math-description Description \subsection math-description Description
`math` is used to perform mathematical calculations. It supports all the usual operations such as addition, subtraction, etc. As well as functions like `abs()`, `sqrt()` and `log2()`. It also has several basic statistical functions like `min()`, `max`, and `avg()`. Internally all calculations are performed using floating point representation. Integer values are first converted to floating point before being used. The default output format is integer which causes floating point results to be rounded down to the nearest integer. The `--scale` option can be used to get floating point output. `math` is used to perform mathematical calculations. It supports all the usual operations such as addition, subtraction, etc. As well as functions like `abs()`, `sqrt()` and `log2()`. Internally all calculations are performed using floating point representation. Integer values are first converted to floating point before being used. The default output format is integer which causes floating point results to be rounded down to the nearest integer. The `--scale` option can be used to get floating point output.
The `math` command is based on the MuParser library which is documented <a href="http://beltoforion.de/article.php?a=muparser&hl=en&p=features&s=idPageTop#idPageTop">here</a>. The stock MuParser does not support the modulo, `%`, operator but fish implements it using integer semantics.
Keep in mind that parameter expansion takes 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. Keep in mind that parameter expansion takes 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.
The `math` command can evaluate multiple expressions separated by commas. The result of each expression is written on a separate line. This means you can evaluate multiple expressions and capture the results in a single invocation just like you can with commands like `string`. See the examples below.
The following options are available: The following options are available:
- `-sN` or `--scale=N` sets the scale of the result. `N` must be an integer and defaults to zero. 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. - `-sN` or `--scale=N` sets the scale of the result. `N` must be an integer and defaults to zero. 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.
@ -23,11 +19,48 @@ The following options are available:
If the expression is successfully evaluated the return `status` is zero (success) else one. If the expression is successfully evaluated the return `status` is zero (success) else one.
\subsection math-constants
`math` knows the following constants:
- `e` - Euler's number.
- `pi` - You know that one. Half of Tau.
Use them without a leading `$`.
\subsection math-functions
`math` supports the following functions:
- `abs`
- `acos`
- `asin`
- `atan`
- `atan2`
- `ceil`
- `cos`
- `cosh`
- `exp` - the base-e exponential function
- `fac` - factorial
- `floor`
- `ln`
- `log` or `log10` - the base-10 logarithm
- `ncr`
- `npr`
- `pow(x,y)` returns x to the y (and can be written as `x ^ y`)
- `sin`
- `sinh`
- `sqrt`
- `tan`
- `tanh`
All of the trigonometric functions use radians.
\subsection math-example Examples \subsection math-example Examples
`math 1+1` outputs 2. `math 1+1` outputs 2.
`math status - 128` outputs the numerical exit status of the last command minus 128. `math $status - 128` outputs the numerical exit status of the last command minus 128.
`math 10 / 6` outputs `1`. `math 10 / 6` outputs `1`.
@ -35,23 +68,11 @@ If the expression is successfully evaluated the return `status` is zero (success
`math -s3 10 / 6` outputs `1.666`. `math -s3 10 / 6` outputs `1.666`.
Capture the result of three expressions: `math "sin(pi)"` outputs `0`.
\fish
$ set x 5
$ set results (math "$x+$x, $x*3, $x^2")
$ set --show results
$results: not set in local scope
$results: set in global scope, unexported, with 3 elements
$results[1]: length=2 value=|10|
$results[2]: length=2 value=|15|
$results[3]: length=2 value=|25|
$results: not set in universal scope
\endfish
\subsection math-notes Compatibility notes \subsection math-notes Compatibility notes
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 MuParser library and evaluates the expression without the involvement of any external commands. 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.
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. 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.