Sha256: b90d16b8f9dc1a6035554fd92c2610ac56a4a406cebf275b0d982e4dfc94f262

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# Approminately equals returns true when values are close enough, within some
# distance epsilon above or below the given value. Three methods and a class
# variable are added to the Numeric class.
#
# @@epsilon, and two class methods, Numeric.epsilon and Numeric.epsilon= get
# and set the maximum difference between two values for them to be
# approximately equal.
#
# approximately_equals? and the aliased operator =~ return true if two values
# are approximately equal.

# Behavor added to Numeric by eymiha_math.
class Numeric
  
  # The initial default distance from a Numeric below which it is considered
  # to be approximately equal to a another Numeric.
  @@epsilon = 0.0000000001
  
  # The default distance from a Numeric below which it is considered to be
  # approximately equal to a another Numeric.
  def Numeric.epsilon
    @@epsilon
  end
  
  # Sets the default distance for approximately equals comparisons.
  def Numeric.epsilon=(epsilon)
    @@epsilon = epsilon.abs
  end

  # returns true if the difference between the instance and the given Numeric
  # is less than the given distance.
  def approximately_equals?(value,epsilon=@@epsilon)
    (self - value).abs < epsilon
  end
  
  alias =~ approximately_equals?
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eymiha_math-0.1.1 lib/approximately_equals.rb
eymiha_math-1.0.0 lib/eymiha/math/approximately_equals.rb
eymiha_math-1.0.1 lib/eymiha/math/approximately_equals.rb