Sha256: d258d0053e36877f1c2bfd1ced7be68a35bc452af1f2943d913f58002aa45ca7

Contents?: true

Size: 762 Bytes

Versions: 4

Compression:

Stored size: 762 Bytes

Contents

require 'facet/float/round_to'

class Float
  # Determines if another number is approximately equal
  # within a given _n_th degree. Defaults to 100ths
  # if the degree is not specified.
  def approx?( x, n=0.01 )
    return(self == x) if n == 0
    self.round_to(n) == x.to_f.round_to(n)
  end
end

class Numeric
  # To properly support Float#approx?,
  # Numeric must also be augmented.
  def approx?(*args)
    self.to_f.approx?(*args)
  end
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCFloat < Test::Unit::TestCase

    def test_approx?
      f = 10.006
      assert( f.approx?(10.01) )
      assert( f.approx?(10, 0.1) )
    end

  end

=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/float/approx.rb
facets-1.0.3 packages/core/lib/facet/float/approx.rb
facets-1.1.0 lib/facet/float/approx.rb
facets-1.2.0 lib/facets/core/float/approx.rb