Module: Philiprehberger::Approx::RSpecMatchers

Defined in:
lib/philiprehberger/approx/rspec_matchers.rb

Defined Under Namespace

Classes: BeApproxMatcher, BeApproxWithinMatcher

Instance Method Summary collapse

Instance Method Details

#be_approx(expected, epsilon: Float::EPSILON, rel_tol: 0) ⇒ Object

Matcher for approximate equality using epsilon

When rel_tol is non-zero, the matcher passes if either the absolute epsilon or the relative tolerance is met (Python math.isclose semantics).

Examples:

expect(1.0).to be_approx(1.0 + 1e-10)
expect(1.0).to be_approx(1.05, epsilon: 0.1)
expect(1e12).to be_approx(1e12 + 1e9, rel_tol: 0.01)


15
16
17
# File 'lib/philiprehberger/approx/rspec_matchers.rb', line 15

def be_approx(expected, epsilon: Float::EPSILON, rel_tol: 0)
  BeApproxMatcher.new(expected, epsilon, rel_tol)
end

#be_approx_within(expected, abs: nil, rel: nil, percent: nil) ⇒ Object

Matcher for approximate equality using abs, rel, or percent tolerance

Examples:

expect(1.0).to be_approx_within(1.001, abs: 0.01)
expect(1_000_000.0).to be_approx_within(1_000_001.0, rel: 1e-5)
expect(100.0).to be_approx_within(105.0, percent: 10)


25
26
27
# File 'lib/philiprehberger/approx/rspec_matchers.rb', line 25

def be_approx_within(expected, abs: nil, rel: nil, percent: nil)
  BeApproxWithinMatcher.new(expected, abs, rel, percent)
end