Class: Given::Fuzzy::FuzzyNumber
- Inherits:
-
Object
- Object
- Given::Fuzzy::FuzzyNumber
- Defined in:
- lib/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.
10 11 12 13 |
# File 'lib/given/fuzzy_number.rb', line 10 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.
8 9 10 |
# File 'lib/given/fuzzy_number.rb', line 8 def delta_amount @delta_amount end |
#exact_value ⇒ Object (readonly)
Returns the value of attribute exact_value.
8 9 10 |
# File 'lib/given/fuzzy_number.rb', line 8 def exact_value @exact_value end |
Instance Method Details
#==(other) ⇒ Object
True if the other number is in range of the fuzzy number.
32 33 34 |
# File 'lib/given/fuzzy_number.rb', line 32 def ==(other) (other - exact_value).abs <= delta_amount end |
#delta(delta) ⇒ Object
Set the delta for a fuzzy number.
41 42 43 44 |
# File 'lib/given/fuzzy_number.rb', line 41 def delta(delta) @delta_amount = delta.abs self end |
#epsilon(neps) ⇒ Object
Specifying the number of epsilons to be used in setting the delta.
54 55 56 |
# File 'lib/given/fuzzy_number.rb', line 54 def epsilon(neps) delta(exact_value * (neps * Float::EPSILON)) end |
#exactly_equals?(other) ⇒ Boolean
15 16 17 18 19 |
# File 'lib/given/fuzzy_number.rb', line 15 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.
27 28 29 |
# File 'lib/given/fuzzy_number.rb', line 27 def high_limit exact_value + delta_amount end |
#low_limit ⇒ Object
Low limit of the fuzzy number.
22 23 24 |
# File 'lib/given/fuzzy_number.rb', line 22 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.
48 49 50 |
# File 'lib/given/fuzzy_number.rb', line 48 def percent(percentage) delta(exact_value * (percentage / 100.0)) end |
#to_s ⇒ Object
36 37 38 |
# File 'lib/given/fuzzy_number.rb', line 36 def to_s "<Approximately #{exact_value} +/- #{delta_amount}>" end |