Permalink
Browse files

Simplified fuzzy number implementation

  • Loading branch information...
1 parent e6c872d commit b33c89961a2e43ed4212ea5171a60e3b91a314da @jimweirich committed Jan 14, 2013
@@ -23,6 +23,8 @@
(puts "Ha ha world", ! true)
}
+ Then { Math.sqrt(10) == about(3.1623).percent(0.0001) }
+
describe "Error Examples" do
When(:result) { fail "OUCH" }
Then { result == :ok }
@@ -6,15 +6,9 @@ class FuzzyNumber # :nodoc:
DEFAULT_EPSILON = 10 * Float::EPSILON
- def initialize(number, options=nil)
+ def initialize(number)
@number = number
- case options
- when Numeric
- @delta = options
- when Hash
- @delta = delta_from_options(options, number)
- end
- @delta ||= (number * DEFAULT_EPSILON)
+ @delta = number * DEFAULT_EPSILON
end
def ==(other)
@@ -25,42 +19,19 @@ def to_s
"<Approximately #{@number} +/- #{@delta}>"
end
- private
-
- OPTIONS = {
- epsilon: ->(neps, number) { number * (neps * Float::EPSILON) },
- percent: ->(percent, number) { number * (percent / 100.0) },
- delta: ->(delta, number) { delta },
- }
-
- def delta_from_options(options, number)
- validate_hash_options(options)
- key = options.keys.first
- OPTIONS[key].(options[key], number)
+ def delta(delta)
+ @delta = delta
+ self
end
- def validate_hash_options(options)
- validate_only_one_option(options)
- validate_known_options(options)
+ def percent(percentage)
+ @delta = @number = (percentage / 100.0)
+ self
end
- def validate_only_one_option(options)
- if options.size < 1
- fail ArgumentError, "No options given"
- end
- if options.size > 1
- fail ArgumentError, "Too many options: '#{options.keys.join(', ')}'"
- end
- end
-
- VALID_KEYS = OPTIONS.keys
-
- def validate_known_options(options)
- options.keys.each do |k|
- if ! VALID_KEYS.include?(k)
- fail ArgumentError, "Invalid option: '#{k}'"
- end
- end
+ def epsilon(neps)
+ @delta = @number * (neps * Float::EPSILON)
+ self
end
end
@@ -6,23 +6,9 @@
describe "fixed deltas" do
- context "when created with non-hash delta" do
- Given(:delta) { 0.0001 }
- Given(:number) { about(10, delta) }
-
- Then { 10 == number }
- Then { number == 10 }
-
- Then { (10 + 0.0001) == number }
- Then { (10 - 0.0001) == number }
-
- Then { (10 + 0.000100001) != number }
- Then { (10 - 0.000100001) != number }
- end
-
context "when created with explicit delta" do
Given(:exact_number) { 10 }
- Given(:number) { about(exact_number, delta: 0.001) }
+ Given(:number) { about(exact_number).delta(0.001) }
Then { exact_number == number }
@@ -36,7 +22,7 @@
describe "percentage deltas" do
Given(:exact_number) { 1 }
- Given(:number) { about(exact_number, percent: 25) }
+ Given(:number) { about(exact_number).percent(25) }
Then { exact_number == number }
@@ -82,32 +68,13 @@
context "when created with small epsilon" do
Given(:neps) { 100 }
Given(:exact_number) { 10 }
- Given(:number) { about(exact_number, epsilon: neps) }
+ Given(:number) { about(exact_number).epsilon(neps) }
Then { exact_number == number }
end
end
describe "#to_s" do
- Given(:number) { about(10, delta: 0.0001) }
+ Given(:number) { about(10).delta(0.0001) }
Then { number.to_s == "<Approximately 10 +/- 0.0001>" }
end
-
- describe "invalid options" do
- context "with an illegal option" do
- When(:result) { about(10, junk: 1) }
- Then { result == have_failed(ArgumentError, /invalid.*junk/i) }
- end
-
- context "with too many options" do
- When(:result) { about(10, epsilon: 1, delta: 10) }
- Then { result == have_failed(ArgumentError, /too many/i) }
- And { result == have_failed(ArgumentError, /epsilon/i) }
- And { result == have_failed(ArgumentError, /delta/i) }
- end
-
- context "with no options" do
- When(:result) { about(10, {}) }
- Then { result == have_failed(ArgumentError, /no options/i) }
- end
- end
end

0 comments on commit b33c899

Please sign in to comment.