You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like a way to auto-calculate the margin for assert_almost_equals() based on lua's default tostring() decimal precision. This lets you copy and paste the result of a known good calculation and use it as the expected result for an assert.
For example:
print(3/7) -- prints "0.42857142857143"luaunit.assert_almost_equals(3/7, 0.42857142857143, 'default') -- passes
I came up with a possible implementation (posted here under public domain).
localdefault_margin_precision=#(tostring(math.pi)) -1-- sub 1 for dot charlocalfunctioncalc_margin(n, prec)
prec= (prec==nilanddefault_margin_precision) orprecassert(prec>0, "precision must be an int greater than zero")
localdigits_over_zero=math.ceil(math.log(math.abs(n), 10))
localdigits_below_zero=prec-digits_over_zeroreturn1.0/10^math.min(digits_below_zero, math.max(0, prec-1))
endassert(calc_margin(3.4883172844444) ==0.0000000000001)
assert(calc_margin(34883.172844444) ==00000.000000001)
assert(calc_margin(0.3488317284444) ==0.0000000000001)
assert(calc_margin(348831728444.44) ==000000000000.01)
assert(calc_margin(0.0000348831728) ==0.0000000000001)
luaunit.assert_almost_equals(3/7, 0.42857142857143, calc_margin(0.42857142857143))
The text was updated successfully, but these errors were encountered:
I'd like a way to auto-calculate the margin for
assert_almost_equals()
based on lua's defaulttostring()
decimal precision. This lets you copy and paste the result of a known good calculation and use it as the expected result for an assert.For example:
I came up with a possible implementation (posted here under public domain).
The text was updated successfully, but these errors were encountered: