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