Exception: CloseAssay

Inherits:
CompareAssay show all
Defined in:
lib/assay/close_assay.rb

Overview

Relative approximation, sometimes refered to as *within epsilon*.

Constant Summary

Constants inherited from Assertion

Assertion::SIZE_LIMIT

Constants included from Assay::Assertable

Assay::Assertable::SIZE_LIMIT

Class Method Summary collapse

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.assert_message(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.

Returns:

  • (Boolean)


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