Class: Philiprehberger::Approx::Comparator
- Inherits:
-
Object
- Object
- Philiprehberger::Approx::Comparator
- Defined in:
- lib/philiprehberger/approx/comparator.rb
Instance Attribute Summary collapse
-
#epsilon ⇒ Object
readonly
Returns the value of attribute epsilon.
-
#relative ⇒ Object
readonly
Returns the value of attribute relative.
Instance Method Summary collapse
-
#assert_near(a, b) ⇒ Object
Assert that two values are approximately equal, raising on mismatch.
-
#assert_within(a, b) ⇒ Object
Assert that two values pass within?, raising on mismatch.
-
#between?(value, min, max) ⇒ Boolean
Check if a numeric value lies within [min, max] with epsilon slack.
-
#clamp(value, target) ⇒ Numeric
Snap a value to target if approximately equal, otherwise return unchanged.
-
#equal?(a, b) ⇒ Boolean
Check if two values are approximately equal using configured tolerances.
-
#initialize(epsilon: 1e-9, relative: nil) ⇒ Comparator
constructor
Create a reusable comparator with preset tolerances.
-
#near?(a, b) ⇒ Boolean
Alias for #equal? matching the module-level near? naming.
-
#relative_equal?(a, b) ⇒ Boolean
Check if two values are approximately equal using relative tolerance.
-
#within?(a, b) ⇒ Boolean
Check using combined absolute + relative tolerance from the configured comparator.
-
#zero?(value) ⇒ Boolean
Check if a numeric value is approximately zero.
Constructor Details
#initialize(epsilon: 1e-9, relative: nil) ⇒ Comparator
Create a reusable comparator with preset tolerances
12 13 14 15 |
# File 'lib/philiprehberger/approx/comparator.rb', line 12 def initialize(epsilon: 1e-9, relative: nil) @epsilon = epsilon @relative = relative end |
Instance Attribute Details
#epsilon ⇒ Object (readonly)
Returns the value of attribute epsilon.
6 7 8 |
# File 'lib/philiprehberger/approx/comparator.rb', line 6 def epsilon @epsilon end |
#relative ⇒ Object (readonly)
Returns the value of attribute relative.
6 7 8 |
# File 'lib/philiprehberger/approx/comparator.rb', line 6 def relative @relative end |
Instance Method Details
#assert_near(a, b) ⇒ Object
Assert that two values are approximately equal, raising on mismatch
55 56 57 58 59 |
# File 'lib/philiprehberger/approx/comparator.rb', line 55 def assert_near(a, b) return if equal?(a, b) raise Error, "expected #{a.inspect} to be near #{b.inspect} (epsilon: #{@epsilon}, relative: #{@relative})" end |
#assert_within(a, b) ⇒ Object
Assert that two values pass within?, raising on mismatch
102 103 104 |
# File 'lib/philiprehberger/approx/comparator.rb', line 102 def assert_within(a, b) Approx.assert_within(a, b, abs: @epsilon, rel: @relative) end |
#between?(value, min, max) ⇒ Boolean
Check if a numeric value lies within [min, max] with epsilon slack
93 94 95 |
# File 'lib/philiprehberger/approx/comparator.rb', line 93 def between?(value, min, max) Approx.between?(value, min, max, epsilon: @epsilon) end |
#clamp(value, target) ⇒ Numeric
Snap a value to target if approximately equal, otherwise return unchanged
75 76 77 |
# File 'lib/philiprehberger/approx/comparator.rb', line 75 def clamp(value, target) Approx.clamp(value, target, epsilon: @epsilon) end |
#equal?(a, b) ⇒ Boolean
Check if two values are approximately equal using configured tolerances
22 23 24 25 26 27 28 29 30 |
# File 'lib/philiprehberger/approx/comparator.rb', line 22 def equal?(a, b) if @relative && @epsilon Approx.within?(a, b, abs: @epsilon, rel: @relative) elsif @relative Approx.relative_equal?(a, b, tolerance: @relative) else Approx.equal?(a, b, epsilon: @epsilon) end end |
#near?(a, b) ⇒ Boolean
Alias for #equal? matching the module-level near? naming
37 38 39 |
# File 'lib/philiprehberger/approx/comparator.rb', line 37 def near?(a, b) equal?(a, b) end |
#relative_equal?(a, b) ⇒ Boolean
Check if two values are approximately equal using relative tolerance
66 67 68 |
# File 'lib/philiprehberger/approx/comparator.rb', line 66 def relative_equal?(a, b) Approx.relative_equal?(a, b, tolerance: @relative || @epsilon) end |