require 'nano/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