Extra Functions and Operators » Histórico » Versión 10
Federico Vera, 2018-08-02 02:20
1 | 1 | Federico Vera | # Extra Functions and Operators |
---|---|---|---|
2 | |||
3 | 3 | Federico Vera | {{>toc}} |
4 | |||
5 | 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. |
6 | 1 | Federico Vera | |
7 | ## Comparison Operators |
||
8 | 2 | Federico Vera | Aside from the arithmetic operators several comparison operators are included: `<`, `<=`, `>`, `>=`, `!=` and `==`. |
9 | 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: |
||
10 | 1 | Federico Vera | |
11 | 9 | 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#L150) and `false` otherwise. |
12 | 1 | Federico Vera | |
13 | ### Precedence |
||
14 | Operator precedence is altered when using the comparison operators, the new precedence is: |
||
15 | ~~~ |
||
16 | FIRST-> * / % - + > >= < <= ¬ & | == != <-LAST |
||
17 | ----- --- --------- - - - ----- |
||
18 | ^^^ The dashes indicate groups with the same precedence |
||
19 | ~~~ |
||
20 | |||
21 | 7 | Federico Vera | ### Warning! |
22 | See [[Warnings#Concatenating-comparison-operators|Warning -> Concatenating Comparison Operators]] |
||
23 | |||
24 | 1 | Federico Vera | ## Boolean Functions |
25 | Even though we have boolean operators, there are also a number of boolean functions that can be used (some people prefer functions) |
||
26 | * `and(a, b)`: Logical and _equivalent to `a & b`_ |
||
27 | * `or(a, b)`: Logical or _equivalent to `a | b`_ |
||
28 | * `xor(a, b)`: Exclusive or |
||
29 | * `nand(a, b)`: _equivalent to `¬(a & b)`_ |
||
30 | * `nor(a, b)`: _equivalent to `¬(a | b)`_ |
||
31 | * `xnor(a, b)`: Negated exclusive or |
||
32 | * `not(a)`: _equivalent to `¬a`_ |
||
33 | |||
34 | ## Boolean Constants |
||
35 | * `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. |
||
36 | * `false()`: false value `0.0` |
||
37 | |||
38 | ## Extra Functions |
||
39 | * `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()` |
||
40 | * `equals(a, b)`: Equivalent to `a == b`, so the same conditions apply. |
||
41 | 4 | Federico Vera | * `isNan(a)`: Tells if a value is `Not A Number` |
42 | * `min(x, y)`: Returns the smallest (closest to negative infinity) of two numbers. |
||
43 | * `max(x, y)`: Returns the largest (closest to positive infinity) of two numbers. |
||
44 | 5 | Federico Vera | * `lcm(x, y)`: Returns the Least Common Multiple of two numbers. |
45 | * `gcd(x, y)`: Returns the Greatest Common Divisor of two numbers. |
||
46 | * `round(x)`: Returns the closest integer of `x`. |
||
47 | 10 | Federico Vera | * `deg2rad(x)`: Returns the value of `x` expressed in radians. |
48 | * `rad2deg(x)`: Returns the value of `x` expressed in degrees. |
||
49 | 6 | Federico Vera | |
50 | 1 | Federico Vera | ## Signal Functions |
51 | 10 | Federico Vera | * `sinc(t)`: [Cardinal sine of `t`](https://en.wikipedia.org/wiki/Sinc_function) (not normalized), equivalent to `sin(t)/t` |
52 | * `triangle(t)`: Triangle wave |
||
53 | * `sawtooth(t)`: Sawtooth wave |
||
54 | * `heavyside(t)`: Heavyside Step function |
||
55 | * `rectangle(t, X, Y)`: Rectangular function |
||
56 | 4 | Federico Vera | |
57 | ## Extra constants |
||
58 | * `inf()`: Retrieves the value of `Double.POSITIVE_INFINITY` |
||
59 | 1 | Federico Vera | |
60 | 8 | Federico Vera | ## Expression based functions |
61 | * It's now possible to create expression based functions! See: [[Expression Based Functions]] |
||
62 | |||
63 | 1 | Federico Vera | ## Code |
64 | 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). |