Class OperatorsComparison

java.lang.Object
net.objecthunter.exp4j.extras.OperatorsComparison

public final class OperatorsComparison extends Object
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.

The precedence of these operators is as follows:
  • 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 Details

    • PRECEDENCE_COMPARISON

      public static final int PRECEDENCE_COMPARISON
      Precedence for the comparison operators (Value 450).
      See Also:
    • PRECEDENCE_EQUAL

      public static final int PRECEDENCE_EQUAL
      Precedence for the equality operators (Value 50).
      See Also:
    • OP_GT

      public static final Operator OP_GT
      Operator Greater Than >.
      Since:
      0.6-riddler
    • OP_GOE

      public static final Operator OP_GOE
      Operator Greater Or Equal Than >=.
      Since:
      0.6-riddler
    • OP_LT

      public static final Operator OP_LT
      Operator Less Than <.
      Since:
      0.6-riddler
    • OP_LOE

      public static final Operator OP_LOE
      Operator Less Or Equal Than <=.
      Since:
      0.6-riddler
    • OP_EQU

      public static final Operator OP_EQU
      Operator Equality =.
      Since:
      0.6-riddler
    • OP_NEQ

      public static final Operator OP_NEQ
      Operator Inequality !=.
      Since:
      0.6-riddler
    • EQUALITY_THRESHOLD

      public static final double EQUALITY_THRESHOLD
      This is the threshold used to consider values equal, that is, if two values a and b 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