Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat req] assert_almost_equals() margin relative to precision of expected number #157

Open
midrare opened this issue May 6, 2023 · 0 comments

Comments

@midrare
Copy link

midrare commented May 6, 2023

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).

local default_margin_precision = #(tostring(math.pi)) - 1  -- sub 1 for dot char

local function calc_margin(n, prec)
    prec = (prec == nil and default_margin_precision) or prec
    assert(prec > 0, "precision must be an int greater than zero")
    local digits_over_zero = math.ceil(math.log(math.abs(n), 10))
    local digits_below_zero = prec - digits_over_zero
    return 1.0 / 10^math.min(digits_below_zero, math.max(0, prec - 1))
end

assert(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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant