Eq can perform simple calculations, by calling it on the command line and giving it an expression as parameter.
$ eq exacteval "3 + 3"
6
Here you can see the term exact eval. There is two important command for program/expression evaluation.
exacteval : evaluate without compromising precision, no operation involving float are performed.
eval : try to reduce an expression as much as possible, without regard for precision. If you want to obtain a numerical answer, that’s the command to use.
Let’s illustrate the difference
$ eq exacteval "3 / 9"
1
---
3
$ eq eval "3 / 9"
0.3333333333333333
And for some trigonometry :
$ eq exacteval "sin(2)"
sin(2)
$ eq eval "sin(2)"
0.9092974268256817
You can use the usual operators (+, -, *, /) for addition, subtraction, multiplication and division respectively.
You can also use some comparison operators :
- = equality
- /= inequality.
- < lower than
- > greater than
- >= greater or equal
- <= lower or equal
$ eq eval "3 * 6 < 15"
false
$ eq eval "3 * 6 > 15"
true
Some time you won’t have documentation directly available. But you still can ask Eq which operators does he knows.
$ eq show --operators
Supported operators :
=====================
Binary operators (Priority - name - description)
------------------------------------------------
8 - := - Attribution operator
8 - :> - Lazy attribution operator
7 - :: - List appending operator
6 - & - Logical and operator
6 - | - Logical or operator
5 - = - Equality operator
5 - /= - Different operator
5 - < - Lower than operator
5 - > - Greater than operator
5 - >= - Greater or equal operator
5 - <= - Lower or equal operator
4 - + - Addition operator
4 - - - Substraction operator
3 - * - Multiplication operator
3 - / - Division/fraction operator
2 - ^ - Power operator
Unary operators (name - description)
------------------------------------
- - Negation operator, put it before expression (-x)
! - Factorial operator, put it after expression (x!)