Class: RSpec::Given::Fuzzy::FuzzyNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/given/fuzzy_number.rb

Constant Summary collapse

DEFAULT_EPSILON =
10 * Float::EPSILON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exact_value) ⇒ FuzzyNumber

Returns a new instance of FuzzyNumber.



11
12
13
14
# File 'lib/rspec/given/fuzzy_number.rb', line 11

def initialize(exact_value)
  @exact_value = exact_value
  @delta_amount = exact_value * DEFAULT_EPSILON
end

Instance Attribute Details

#delta_amountObject (readonly)

Returns the value of attribute delta_amount.



9
10
11
# File 'lib/rspec/given/fuzzy_number.rb', line 9

def delta_amount
  @delta_amount
end

#exact_valueObject (readonly)

Returns the value of attribute exact_value.



9
10
11
# File 'lib/rspec/given/fuzzy_number.rb', line 9

def exact_value
  @exact_value
end

Instance Method Details

#==(other) ⇒ Object

True if the other number is in range of the fuzzy number.



33
34
35
# File 'lib/rspec/given/fuzzy_number.rb', line 33

def ==(other)
  (other - exact_value).abs <= delta_amount
end

#delta(delta) ⇒ Object

Set the delta for a fuzzy number.



42
43
44
45
# File 'lib/rspec/given/fuzzy_number.rb', line 42

def delta(delta)
  @delta_amount = delta.abs
  self
end

#epsilon(neps) ⇒ Object

Specifying the number of epsilons to be used in setting the delta.



55
56
57
# File 'lib/rspec/given/fuzzy_number.rb', line 55

def epsilon(neps)
  delta(exact_value * (neps * Float::EPSILON))
end

#exactly_equals?(other) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/rspec/given/fuzzy_number.rb', line 16

def exactly_equals?(other)
  other.is_a?(self.class) &&
    exact_value == other.exact_value &&
    delta_amount == other.delta_amount
end

#high_limitObject

High limit of the fuzzy number.



28
29
30
# File 'lib/rspec/given/fuzzy_number.rb', line 28

def high_limit
  exact_value + delta_amount
end

#low_limitObject

Low limit of the fuzzy number.



23
24
25
# File 'lib/rspec/given/fuzzy_number.rb', line 23

def low_limit
  exact_value - delta_amount
end

#percent(percentage) ⇒ Object

Specifying a percentage of the exact number to be used in setting the delta.



49
50
51
# File 'lib/rspec/given/fuzzy_number.rb', line 49

def percent(percentage)
  delta(exact_value * (percentage / 100.0))
end

#to_sObject



37
38
39
# File 'lib/rspec/given/fuzzy_number.rb', line 37

def to_s
  "<Approximately #{exact_value} +/- #{delta_amount}>"
end