Package net.objecthunter.exp4j.extras
Class OperatorsComparison
java.lang.Object
net.objecthunter.exp4j.extras.OperatorsComparison
This class contains the implementation of comparison and equality operators.
The returned values will always be 1.0 for true
and 0.0 for
false
.
- All comparison operators have the same precedence
- Comparison operators have higher precedence than boolean operators
- Comparison operators have lower precedence than arithmetic operators
- Equality operators have the lowest precedence (they should always be the last ones to be evaluated)
- Equality operators will consider numbers closer than
EQUALITY_THRESHOLD
to be equal (and viceversa)
To clarify the full evaluation order is: FIRST-> * / % - + > >= < <= ¬ & | == != <-LAST ----- --- --------- - - - ----- ^^^ The dashes indicate the ones with the same precedence So: a + b * c > d / e & f < g == (((a + (b * c)) > (d / e)) & (f < g))
WARNING:
Concatenating comparison operators should be avoided!
This is a brutal and very common error. Imagine the following equation:
a < b < c
, now let's set a = 1
, b = 3
and
c = 2
so it will roughly translate to 1 < 3 < 2
. Those of you
not familiarized with operator precedence evaluation might think that this
will result in false
but it will actually return true
. Why?
Well:
a < b < c -> ((a < b) < c)
So the example will become:
1 < 3 < 2 -> ((1 < 3) < 2) -> ((1) < 2) -> 1
So the morality of this tale is: if unsure, DON'T CONCATENATE OPERATORS
- Author:
- Federico Vera <[email protected]>
-
Field Summary
Modifier and TypeFieldDescriptionstatic final double
This is the threshold used to consider values equal, that is, if two valuesa
andb
are separated by less than this threshold they will be considered to be equal, it has a default value of 1.0E-12static final Operator
Operator Equality=
.static final Operator
Operator Greater Or Equal Than>=
.static final Operator
Operator Greater Than>
.static final Operator
Operator Less Or Equal Than<=
.static final Operator
Operator Less Than<
.static final Operator
Operator Inequality!=
.static final int
Precedence for the comparison operators (Value 450).static final int
Precedence for the equality operators (Value 50). -
Method Summary
Modifier and TypeMethodDescriptionstatic Operator
getOperator
(String symbol) Retrieves an operator by its symbol.static Operator[]
Retrieves all the available operators.
-
Field Details
-
PRECEDENCE_COMPARISON
public static final int PRECEDENCE_COMPARISONPrecedence for the comparison operators (Value 450).- See Also:
-
PRECEDENCE_EQUAL
public static final int PRECEDENCE_EQUALPrecedence for the equality operators (Value 50).- See Also:
-
OP_GT
Operator Greater Than>
.- Since:
- 0.6-riddler
-
OP_GOE
Operator Greater Or Equal Than>=
.- Since:
- 0.6-riddler
-
OP_LT
Operator Less Than<
.- Since:
- 0.6-riddler
-
OP_LOE
Operator Less Or Equal Than<=
.- Since:
- 0.6-riddler
-
OP_EQU
Operator Equality=
.- Since:
- 0.6-riddler
-
OP_NEQ
Operator Inequality!=
.- Since:
- 0.6-riddler
-
EQUALITY_THRESHOLD
public static final double EQUALITY_THRESHOLDThis is the threshold used to consider values equal, that is, if two valuesa
andb
are separated by less than this threshold they will be considered to be equal, it has a default value of 1.0E-12- See Also:
-
-
Method Details
-
getOperators
Retrieves all the available operators. -
getOperator
Retrieves an operator by its symbol.
-