mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-30 14:24:35 +08:00
5b1731331e
Otherwise this is printed as "math-syntax" in the man page.
102 lines
3.9 KiB
Plaintext
102 lines
3.9 KiB
Plaintext
\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 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.
|
|
|
|
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 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 and doesn't over/underflow or return NaN the return `status` is zero (success) else one.
|
|
|
|
\subsection math-syntax Syntax
|
|
|
|
`math` knows some operators, constants, functions and can (obviously) read numbers.
|
|
|
|
For numbers, `.` is always the radix character regardless of locale - `2.5`, not `2,5`. Scientific notation (`10e5`) is also available.
|
|
|
|
\subsection math-operators Operators
|
|
|
|
`math` knows the following operators:
|
|
|
|
- `+` for addition and `-` for subtraction.
|
|
|
|
- `*` for multiplication, `/` for division.
|
|
|
|
- `^` for exponentiation.
|
|
|
|
- `%` for modulo.
|
|
|
|
- `(` and `)` for grouping.
|
|
|
|
They are all used in an infix manner - `5 + 2`, not `+ 5 2`.
|
|
|
|
\subsection math-constants 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 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
|
|
|
|
`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`.
|
|
|
|
`math "sin(pi)"` outputs `0`.
|
|
|
|
\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 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.
|
|
|
|
Note that the modulo operator (`x % y`) is not well defined for floating point arithmetic. Fish rounds down all floating point values to the nearest integer before performing the modulo operation. So `10.5 % 6.1` is `4`, regardless of what `--scale` value is in effect.
|