Extra Functions and Operators » Histórico » Versión 2
Federico Vera, 2018-07-08 06:02
1 | 1 | Federico Vera | # Extra Functions and Operators |
---|---|---|---|
2 | |||
3 | 2 | Federico Vera | There are a number of extra functions and operators included in this version of `exp4j`, non of this are enabled by default, they are in the [`net.objecthunter.exp4j.extras`](https://redmine.riddler.com.ar/projects/exp4j/repository/revisions/master/show/src/main/java/net/objecthunter/exp4j/extras) package. |
4 | 1 | Federico Vera | |
5 | ## Comparison Operators |
||
6 | 2 | Federico Vera | Aside from the arithmetic operators several comparison operators are included: `<`, `<=`, `>`, `>=`, `!=` and `==`. |
7 | 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: |
||
8 | 1 | Federico Vera | |
9 | 2 | Federico Vera | 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`)](https://redmine.riddler.com.ar/projects/exp4j/repository/revisions/master/entry/src/main/java/net/objecthunter/exp4j/extras/FunctionsMisc.java#L38) and `false` otherwise. |
10 | 1 | Federico Vera | |
11 | ### Precedence |
||
12 | Operator precedence is altered when using the comparison operators, the new precedence is: |
||
13 | ~~~ |
||
14 | FIRST-> * / % - + > >= < <= ¬ & | == != <-LAST |
||
15 | ----- --- --------- - - - ----- |
||
16 | ^^^ The dashes indicate groups with the same precedence |
||
17 | ~~~ |
||
18 | |||
19 | ## Boolean Functions |
||
20 | Even though we have boolean operators, there are also a number of boolean functions that can be used (some people prefer functions) |
||
21 | * `and(a, b)`: Logical and _equivalent to `a & b`_ |
||
22 | * `or(a, b)`: Logical or _equivalent to `a | b`_ |
||
23 | * `xor(a, b)`: Exclusive or |
||
24 | * `nand(a, b)`: _equivalent to `¬(a & b)`_ |
||
25 | * `nor(a, b)`: _equivalent to `¬(a | b)`_ |
||
26 | * `xnor(a, b)`: Negated exclusive or |
||
27 | * `not(a)`: _equivalent to `¬a`_ |
||
28 | |||
29 | ## Boolean Constants |
||
30 | * `true()`: true value `1.0` (true is actually any value different than zero, but this is the value returned by comparison operators, boolean operators and boolean functions. |
||
31 | * `false()`: false value `0.0` |
||
32 | |||
33 | ## Extra Functions |
||
34 | * `if(e, v_true, v_false)`: Branches the formula, it will be evaluated as `v_true` if `e == true()` and `v_false` if `e == false()` |
||
35 | * `equals(a, b)`: Equivalent to `a == b`, so the same conditions apply. |
||
36 | * `sinc(x)`: [Cardinal sine of `x`](https://en.wikipedia.org/wiki/Sinc_function) (not normalized), equivalent to `sin(x)/x` |
||
37 | |||
38 | ## Code |
||
39 | 2 | Federico Vera | The code for the functions is [here](https://redmine.riddler.com.ar/projects/exp4j/repository/revisions/master/entry/src/main/java/net/objecthunter/exp4j/extras/OperatorsComparison.java), [here](https://redmine.riddler.com.ar/projects/exp4j/repository/revisions/master/entry/src/main/java/net/objecthunter/exp4j/extras/FunctionsMisc.java) and [here](https://redmine.riddler.com.ar/projects/exp4j/repository/revisions/master/entry/src/main/java/net/objecthunter/exp4j/extras/FunctionsBoolean.java). |