Proyecto

General

Perfil

Expressions » Histórico » Versión 2

Federico Vera, 2018-07-12 06:04

1 1 Federico Vera
# Expressions
2 2 Federico Vera
3
{{>toc}}
4
5
All the expressions that are used in `mrft` are backed by project:exp4j, so basically it will handle all the expressions project:exp4j does plus a couple of additions.
6 1 Federico Vera
7
Expressions should be written in `infix notation`, arguments of functions must be surrounded by parentheses and separated by commas; so basically you should write them in the natural way.
8
9
## Operators
10
There are three basic sets of operators available:
11
12
### Arithmetic
13
All of the arithmetic operators are included and available: `+`, `-`, `*`, `/` and of course `%` (modulo).
14
15
### Comparison
16
Aside from the arithmetic operators several comparison operators are included: `<`, `<=`, `>`, `>=`, `!=` and `==`. Please note that since `exp4j` itself has no concept of `boolean`, the results given by all of the comparison operators will be `1.0` if `true` and `0.0` if `false`. Let it be noted that due to rounding, equality operators use a radix in which they consider numbers to be "equal", that is:
17
18
Let `a` and `b` be two real numbers, `a == b` will evaluate to `true` if and only if the distance between `a` and `b` (`dist(a, b) := abs(a - b)`) is less or equal than `1e-12` (`0.000000000001`) and `false` otherwise.
19
20
### Boolean
21
As with comparison operators, boolean operators will consider `false` every value equal to zero (`0 ± 0.000000000001`), and `true` every other value. But they are warrantied to return `1.0` for `true` and `0.0` for `false`. The available operators are: `&` (and), `|` (or), `¬` (not).
22
23
### Precedence
24
For those who don't know, precedence of operators refers to which of the operators will be evaluated first when left alone. I won't bother you with numbers, the order of evaluation is as follows:
25
```
26
FIRST->  * / %  - +  > >= < <=  ¬  &  |  == !=   <-LAST
27
         -----  ---  ---------  -  -  -  -----    
28
         ^^^ The dashes indicate groups with the same precedence
29
```
30
When operators have the same precedence they will be evaluated from left to right.
31
32
***Note:*** as in primary school (and beyond) you can always alter the precedence of operators with the use of parentheses.
33
34
## Functions
35
36
There are quite a few functions available, basically all the ones in [Java Math](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html), and a couple of additions.
37
38
I'll just name them, I'm assuming everyone reading this over the Internet, has Google access to find the ones that don't actually know.
39
40
### Trigonometric
41
Please note that all of the trigonometric functions work in radian: `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `sinh`, `cosh`, `tanh` and `sinc`.
42
43
### Boolean
44
- No arguments: `false`, `true`
45
- One argument: `not`
46
- Two arguments:`and`, `or`, `xor`, `nand`, `nor`, `xnor`, `equals` (this one is not exactly boolean)
47
- Three arguments: `if` which is used like this `if(boolean_exp, val_if_true, val_if_false)` that means that `if(0 < 1, 5, 10) -> 5`
48
49
### Misc
50
`abs`, `log`, `log10`, `log2`, `log1p`, `ceil`, `floor`, `sqrt`, `cbrt`, `pow`, `exp`, `expm1`, `signum`.
51
52
### Random (noise)
53
54
There are 4 random (non deterministic) functions available:
55
- No arguments: 
56
  - `rand`: random uniformly distributed number between `0.0` (inclusive) and `1.0` (exclusive).
57
  - `gaussian`: random gaussianly distributed number with average `0.0` and variance `1.0`
58
- Two arguments:
59
  - `rand2(start, end)`: random uniformly distributed number between `start` (inclusive) and `end` (exclusive).
60
  - `gaussian2(avg, var)`: random gaussianly distributed number with average `avg` and  variance `var`
61
62
### Constants
63
There are two constants implemented as functions (for reasons I will not discuss here), which are `pi` and `e`.
Volver al inicio