aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc_src/math.txt
blob: a22b0e6ea492fce449e3ca4718bf557a30de1a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
\section math math - Perform mathematics calculations

\subsection math-synopsis Synopsis
\fish{synopsis}
math [-sN] EXPRESSION
\endfish

\subsection math-description Description

`math` is used to perform mathematical calculations. It is a very thin wrapper for the bc program, which makes it possible to specify an expression from the command line without using non-standard extensions or a pipeline.

For a description of the syntax supported by math, see the manual for the bc program. 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 have to be escaped.

The following options are available:

- `-sN` Sets the scale of the result. `N` must be an integer and defaults to zero. This simply sets bc's `scale` variable to the provided value. Note that you cannot put a space between `-s` and `N`.

\subsection return-values Return Values

If invalid options or no expression is provided the return `status` is two. If the expression is invalid the  return `status` is three. If bc returns a result of `0` (literally, not `0.0` or similar variants) the return `status` is one otherwise it's zero.

\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`.

\subsection math-cautions Cautions

Note that the modulo operator (`x % y`) is not well defined for floating point arithmetic. The `bc` command produces a nonsensical result rather than emit an error and fail in that case. It doesn't matter if the arguments are integers; e.g., `10 % 4`. You'll still get an incorrect result. Do not use the `-sN` flag with N greater than zero if you want sensible answers when using the modulo operator.