Add absolute_approx to float class : Float Extension « Number « Ruby






Add absolute_approx to float class


class Float
  def absolute_approx(other, epsilon=Float::EPSILON)
    puts((other-self).abs)
    return (other-self).abs <= epsilon
  end
end

puts (1.8 + 0.1).absolute_approx(1.9)               # => true
puts 10e10.absolute_approx(10e10+1e-5)              # => false

puts 98.6.absolute_approx(98.66)

puts 98.6.absolute_approx(98.66, 0.001)

 








Related examples in the same category

1.Add approx method to Float number
2.Compare two floats for a certain precision