module Bang::NumericMixin

Mixin for Numeric class that adds ‘#within?` and `#close?`.

Public Instance Methods

close?(other, epsilon) click to toggle source

Is this value within a given relative ‘epsilon` of another?

@return [true,false] True if within relative epsilon, otherwise false.

# File lib/bang.rb, line 238
def close?(other, epsilon)
  a, b, e = self.to_f, other.to_f, epsilon.to_f

  d = b * e

  (b - d) <= a && (b + d) >= a
end
within?(other, delta) click to toggle source

Is this value within a given absolute ‘delta` of another?

@return [true,false] True if within absolute delta, otherwise false.

# File lib/bang.rb, line 227
def within?(other, delta)
  a, b, d = self.to_f, other.to_f, delta.to_f

  (b - d) <= a && (b + d) >= a   
end