Package

org.hammerlab.math

tolerance

Permalink

package tolerance

Visibility
  1. Public
  2. All

Type Members

  1. final class Geometric extends AnyVal

    Permalink

    Consider Doubles to be "equal" if they are within a factor of ε of one another:

    Consider Doubles to be "equal" if they are within a factor of ε of one another:

    import hammerlab.math.syntax._
    implicit val ε: E = 1e-6
    2.0 === 2.000002      //  true
    2.0 !== 2.0000020001  // false
    2.0 <<< 2.0000020001  //  true
    2.0 <<= 2.0000020001  //  true
    2.02.0000020001  //  true

    Construction is done by assigning an "epsilon" value, as demonstrated above, which is then incremented by 1 to obtain the value that is actually used for comparing ratios. Doubles have 15 decimal-digits'-worth of precision, so an ε of 1e-16 or lower will effectively result in strict equality:

    implicit val ε: E = 1e-15
    // ε: hammerlab.math.tolerance.E = ±1.000000000000001
    
    implicit val ε: E = 1e-16
    // ε: hammerlab.math.tolerance.E = ±1.0

    Note that equality (and by extension, ≤ and ≥) are not transitive! Don't try to use this as an Ordering!

    implicit val ε: E = 1e-2
    2.0  === 2.02  //  true
    2.02 === 2.04  //  true
    2.0  === 2.04  // false!

    Note also that 0 can only === 0; whether a Double shoould be considered fuzzily-equal to 0 is a complicated question (likely requiring tracking of floating-point-arithmetic error) that this simple implementation of fuzzy-equality doesn't attempt to address.

Value Members

  1. object Geometric

    Permalink

Ungrouped