Exception: CloseAssay
- Inherits:
-
CompareAssay
- Object
- Exception
- Assertion
- CompareAssay
- CloseAssay
- Defined in:
- lib/assay/close_assay.rb
Overview
Relative approximation, sometimes refered to as *within epsilon*.
Constant Summary
Constants inherited from Assertion
Constants included from Assay::Assertable
Class Method Summary collapse
-
.assert_message(subject, criterion, delta) ⇒ Object
Failed assertion message.
-
.pass?(subject, criterion, epsilon) ⇒ Boolean
Check assertion.
Methods inherited from Assertion
by_name, by_operator, inherited, register, subclasses
Methods included from Assay::Assertable
#[], #assert!, #assert_message, #assertive_name, #assertor, #fail?, #operator, #pass?, #refute!, #refute_message
Class Method Details
.assert_message(subject, criterion, delta) ⇒ Object
Failed assertion message.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/assay/close_assay.rb', line 29 def self.(subject, criterion, delta) a = subject.inspect b = criterion.inspect e = delta.inspect if [a, b, e].any?{ |e| e.to_s.size > SIZE_LIMIT } "b(1 - e) <= a <= b(1 + e)\na) #{a}\nb) #{b}\nd) #{d}" else "#{b}(1 - #{e}) <= #{a} <= #{b}(1 + #{e})" end end |
.pass?(subject, criterion, epsilon) ⇒ Boolean
Check assertion.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/assay/close_assay.rb', line 12 def self.pass?(subject, criterion, epsilon) #if [subject, criterion, epsilon].all?{ |v| Numeric === v } a, b, e = subject.to_f, criterion.to_f, epsilon.to_f #else # a, b, e = subject, criterion, epsilon #end #(a - b).abs / [a.abs, b.abs].max <= e d = b * e # [a.abs,b.abs].max * e (b - d) <= a && (b + d) >= a end |