Class OperatorsComparison


  • public final class OperatorsComparison
    extends java.lang.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 this 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

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static 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
      static Operator OP_EQU
      Operator Equality =.
      static Operator OP_GOE
      Operator Greater Or Equal Than >=.
      static Operator OP_GT
      Operator Greater Than >.
      static Operator OP_LOE
      Operator Less Or Equal Than <=.
      static Operator OP_LT
      Operator Less Than <.
      static Operator OP_NEQ
      Operator Inequality !=.
      static int PRECEDENCE_COMPARISON
      Precedence for the comparison operators (Value 450).
      static int PRECEDENCE_EQUAL
      Precedence for the equality operators (Value 50).
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Operator getOperator​(java.lang.String symbol)
      Retrieves an operator by it's symbol.
      static Operator[] getOperators()
      Retrieves all the available operators.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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:
        Constant Field Values