mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 04:43:10 +00:00
95162ef19d
This reverts commit 56d9134534
.
An LRU cache in the shell for math seems like overkill.
46 lines
2.7 KiB
Text
46 lines
2.7 KiB
Text
\section math math - Perform mathematics calculations
|
|
|
|
\subsection math-synopsis Synopsis
|
|
\fish{synopsis}
|
|
math [-sN | --scale=N] [--] EXPRESSION
|
|
\endfish
|
|
|
|
\subsection math-description Description
|
|
|
|
`math` is used to perform mathematical calculations. It 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>. You can use bare variable names (i.e., without the dollar-sign). The stock MuParser does not support the modulo, `%` operator but fish implements it using integer semantics.
|
|
|
|
Keep in mind that parameter expansion takes place on any expressions before they 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.
|
|
|
|
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:
|
|
|
|
- `-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.
|
|
|
|
\subsection return-values Return Values
|
|
|
|
If the expression is successfully evaluated the return `status` is zero (success) else one.
|
|
|
|
\subsection math-example Examples
|
|
|
|
`math 1+1` outputs 2.
|
|
|
|
`math status - 128` outputs the numerical exit status of the last command minus 128.
|
|
|
|
`math 10 / 6` outputs `1`.
|
|
|
|
`math -s0 10.0 / 6.0` outputs `1`.
|
|
|
|
`math -s3 10 / 6` outputs `1.666`.
|
|
|
|
Capture the result of three expressions: `set results (math '1+1, 5*3, 10^2')` sets `$results` to 2, 15, and 100.
|
|
|
|
\subsection math-cautions Cautions
|
|
|
|
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.
|
|
|
|
Note that the modulo operator (`x % y`) is not well defined for floating point arithmetic. Fish rounds down all floating point values to nearest int before performing the modulo operation. So `10.5 % 6.1` is `4`.
|
|
|
|
\subsection math-notes Compatibility notes
|
|
|
|
Fish 1.x and 2.x releases relied on the `bc` command for handling math expressions via the `math` command. Starting with fish 3.0.0 fish uses the MuParser library.
|