2020-07-18 06:11:19 +02:00
# math eval
2020-01-29 14:34:54 +01:00
2020-07-18 06:11:19 +02:00
math eval is a command that takes a math expression from the pipeline and evaluates that into a number. It also optionally takes the math expression as an argument.
2020-01-29 14:34:54 +01:00
2020-03-13 18:23:41 +01:00
This command supports the following operations -
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
operations:
2020-01-29 14:34:54 +01:00
* binary operators: +, -, *, /, % (remainder), ^ (power)
* unary operators: +, -, ! (factorial)
2020-06-23 20:21:47 +02:00
functions:
2020-01-29 14:34:54 +01:00
* sqrt, abs
* exp, ln, log10
* sin, cos, tan, asin, acos, atan, atan2
* sinh, cosh, tanh, asinh, acosh, atanh
* floor, ceil, round
* signum
2020-03-11 16:20:22 +01:00
* max(x, ...), min(x, ...): maximum and minimum of 1 or more numbers
2020-01-29 14:34:54 +01:00
constants:
2020-06-23 20:21:47 +02:00
2020-01-29 14:34:54 +01:00
* pi
* e
2020-03-13 18:23:41 +01:00
2020-06-23 20:21:47 +02:00
## Examples
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
```shell
2020-07-18 06:11:19 +02:00
> echo "1+2+3" | math eval
6.0u
2020-06-23 20:21:47 +02:00
```
```shell
2020-07-18 06:11:19 +02:00
> echo "1-2+3" | math eval
2020-06-23 20:21:47 +02:00
2.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo "-(-23)" | math eval
2020-06-23 20:21:47 +02:00
23.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo "5^2" | math eval
2020-06-23 20:21:47 +02:00
25.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo "5^3" | math eval
2020-06-23 20:21:47 +02:00
125.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo "min(5,4,3,2,1,0,-100,45)" | math eval
2020-06-23 20:21:47 +02:00
-100.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo "max(5,4,3,2,1,0,-100,45)" | math eval
2020-06-23 20:21:47 +02:00
45.0
```
```shell
2020-07-18 06:11:19 +02:00
> echo sqrt(2) | math eval
2020-01-29 14:34:54 +01:00
1.414213562373095
2020-06-23 20:21:47 +02:00
```
```shell
2020-07-18 06:11:19 +02:00
> echo pi | math eval
2020-01-29 14:34:54 +01:00
3.141592653589793
2020-06-23 20:21:47 +02:00
```
```shell
2020-07-18 06:11:19 +02:00
> echo e | math eval
2020-01-29 14:34:54 +01:00
2.718281828459045
2020-06-23 20:21:47 +02:00
```
```shell
2020-07-18 06:11:19 +02:00
> echo "sin(pi / 2)" | math eval
2020-06-23 20:21:47 +02:00
1.0
2020-01-29 14:34:54 +01:00
```
2020-06-23 20:21:47 +02:00
```shell
2020-07-18 06:11:19 +02:00
> echo "floor(5999/1000)" | math eval
2020-06-23 20:21:47 +02:00
5.0
2020-01-29 14:34:54 +01:00
```
2020-06-23 20:21:47 +02:00
```shell
> open abc.json
2020-01-29 14:34:54 +01:00
───┬──────
2020-03-13 18:23:41 +01:00
# │ size
2020-01-29 14:34:54 +01:00
───┼──────
2020-03-13 18:23:41 +01:00
0 │ 816
1 │ 1627
2 │ 1436
3 │ 1573
4 │ 935
5 │ 52
6 │ 999
7 │ 1639
2020-01-29 14:34:54 +01:00
───┴──────
2020-06-23 20:21:47 +02:00
```
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
```shell
> open abc.json | format "({size} + 500) * 4"
2020-01-29 14:34:54 +01:00
───┬──────────────────
2020-06-23 20:21:47 +02:00
# │
2020-01-29 14:34:54 +01:00
───┼──────────────────
2020-03-13 18:23:41 +01:00
0 │ (816 + 500) * 4
1 │ (1627 + 500) * 4
2 │ (1436 + 500) * 4
3 │ (1573 + 500) * 4
4 │ (935 + 500) * 4
5 │ (52 + 500) * 4
6 │ (999 + 500) * 4
7 │ (1639 + 500) * 4
2020-01-29 14:34:54 +01:00
───┴──────────────────
2020-06-23 20:21:47 +02:00
```
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
```shell
2020-07-18 06:11:19 +02:00
> open abc.json | format "({size} + 500) * 4" | math eval
2020-01-29 14:34:54 +01:00
───┬───────────
2020-06-23 20:21:47 +02:00
# │
2020-01-29 14:34:54 +01:00
───┼───────────
2020-03-13 18:23:41 +01:00
0 │ 5264.0000
1 │ 8508.0000
2 │ 7744.0000
3 │ 8292.0000
4 │ 5740.0000
5 │ 2208.0000
6 │ 5996.0000
7 │ 8556.0000
2020-01-29 14:34:54 +01:00
───┴───────────
2020-06-23 20:21:47 +02:00
```
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
```shell
2020-07-18 06:11:19 +02:00
> open abc.json | format "({size} - 1000) * 4" | math eval
2020-01-29 14:34:54 +01:00
───┬────────────
2020-06-23 20:21:47 +02:00
# │
2020-01-29 14:34:54 +01:00
───┼────────────
2020-03-13 18:23:41 +01:00
0 │ -736.0000
1 │ 2508.0000
2 │ 1744.0000
3 │ 2292.0000
4 │ -260.0000
5 │ -3792.0000
6 │ -4.0000
7 │ 2556.0000
2020-01-29 14:34:54 +01:00
───┴────────────
```
2020-07-18 06:11:19 +02:00
Note that since `math eval` uses floating-point numbers, the result may not always be precise.
2020-01-29 14:34:54 +01:00
2020-06-23 20:21:47 +02:00
```shell
2020-07-18 06:11:19 +02:00
> echo "floor(5999999999999999999/1000000000000000000)" | math eval
2020-06-23 20:21:47 +02:00
6.0
2020-01-29 14:34:54 +01:00
```