Class: RSpec::Given::Fuzzy::FuzzyNumber
- Inherits:
-
Object
- Object
- RSpec::Given::Fuzzy::FuzzyNumber
- Defined in:
- lib/rspec/given/fuzzy_number.rb
Constant Summary collapse
- DEFAULT_EPSILON =
10 * Float::EPSILON
Instance Attribute Summary collapse
-
#delta_amount ⇒ Object
readonly
Returns the value of attribute delta_amount.
-
#exact_value ⇒ Object
readonly
Returns the value of attribute exact_value.
Instance Method Summary collapse
-
#==(other) ⇒ Object
True if the other number is in range of the fuzzy number.
-
#delta(delta) ⇒ Object
Set the delta for a fuzzy number.
-
#epsilon(neps) ⇒ Object
Specifying the number of epsilons to be used in setting the delta.
- #exactly_equals?(other) ⇒ Boolean
-
#high_limit ⇒ Object
High limit of the fuzzy number.
-
#initialize(exact_value) ⇒ FuzzyNumber
constructor
A new instance of FuzzyNumber.
-
#low_limit ⇒ Object
Low limit of the fuzzy number.
-
#percent(percentage) ⇒ Object
Specifying a percentage of the exact number to be used in setting the delta.
- #to_s ⇒ Object
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_amount ⇒ Object (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_value ⇒ Object (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
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_limit ⇒ Object
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_limit ⇒ Object
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_s ⇒ Object
37 38 39 |
# File 'lib/rspec/given/fuzzy_number.rb', line 37 def to_s "<Approximately #{exact_value} +/- #{delta_amount}>" end |