Class: Float

Inherits
Object show all
Defined in
lib/kyanite/numeric/float.rb

Numeric Additions

Kyanite definitions

Numeric Integer Float

Kyanite tests and examples

TestKyaniteNumeric

Usage

require 'kyanite/numeric'

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) fuzzy_equal?(other, error_max = 0.000001)

Fuzzy comparison of Floats. Example:

(98.6).fuzzy_equal?(98.66)          => false
(98.6).fuzzy_equal?(98.66, 0.001)   => true

Parameters:

  • other (Float)

    Value to compare with

  • error_max (Float) (defaults to: 0.000001)

    Maximum relative error

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/kyanite/numeric/float.rb', line 22

def fuzzy_equal?(other, error_max=0.000001)
  diff = other - self
  return true if diff.abs <= Float::EPSILON
  relative_error = (diff / (self > other ? self : other)).abs
  #puts relative_error
  return relative_error <= error_max
end