Proyecto

General

Perfil

Extra Functions and Operators » Histórico » Versión 4

Federico Vera, 2018-07-17 03:57

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 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.
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
## Boolean Functions
22
Even though we have boolean operators, there are also a number of boolean functions that can be used (some people prefer functions)
23
* `and(a, b)`: Logical and _equivalent to `a & b`_
24
* `or(a, b)`: Logical or _equivalent to `a | b`_
25
* `xor(a, b)`: Exclusive or
26
* `nand(a, b)`: _equivalent to `¬(a & b)`_
27
* `nor(a, b)`: _equivalent to `¬(a | b)`_
28
* `xnor(a, b)`: Negated exclusive or 
29
* `not(a)`: _equivalent to `¬a`_
30
31
## Boolean Constants
32
* `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.
33
* `false()`: false value `0.0`
34
35
## Extra Functions
36
* `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()` 
37
* `equals(a, b)`: Equivalent to `a == b`, so the same conditions apply.
38 4 Federico Vera
* `isNan(a)`: Tells if a value is `Not A Number`
39
* `min(x, y)`: Returns the smallest (closest to negative infinity) of two numbers.
40
* `max(x, y)`: Returns the largest (closest to positive infinity) of two numbers.
41 1 Federico Vera
* `sinc(x)`: [Cardinal sine of `x`](https://en.wikipedia.org/wiki/Sinc_function) (not normalized), equivalent to `sin(x)/x`
42 4 Federico Vera
43
## Extra constants
44
* `inf()`: Retrieves the value of `Double.POSITIVE_INFINITY`
45 1 Federico Vera
46
## Code
47 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).
Volver al inicio