Module:Interval edo approximation: Difference between revisions
No edit summary |
No edit summary |
||
| Line 103: | Line 103: | ||
end | end | ||
-- Format a number with sign and | -- Format a number with sign and 2 decimal places | ||
local function format_error(value) | local function format_error(value) | ||
if value >= 0 then | if value >= 0 then | ||
return string.format("+%. | return string.format("+%.2f", value) | ||
else | else | ||
return string.format("%. | return string.format("%.2f", value) | ||
end | end | ||
end | end | ||
| Line 150: | Line 150: | ||
local ratio_cents = cents(ratio) | local ratio_cents = cents(ratio) | ||
-- Include subtitle info in caption to avoid breaking sortable functionality | -- Include subtitle info in caption to avoid breaking sortable functionality | ||
table.insert(output, string.format('|+ EDO Approximations for %s (%. | table.insert(output, string.format('|+ EDO Approximations for %s (%.2f¢)<br/><small>\'\'≤ %dedo, Relative Error ≤ %g%%\'\'</small>', interval_str, ratio_cents, max_edo, tolerance)) | ||
table.insert(output, '|-') | table.insert(output, '|-') | ||
table.insert(output, '! EDO !! class="unsortable" | Step size !! | table.insert(output, '! EDO !! class="unsortable" | Step size !! Cents ([[Cent|¢]]) !! Absolute Error ([[Cent|¢]]) !! [[Relative_interval_error|Relative Error]] ([[Relative_cent|%]])') | ||
for _, result in ipairs(results) do | for _, result in ipairs(results) do | ||
| Line 163: | Line 163: | ||
-- Calculate approximation in cents: steps * (1200/edo) | -- Calculate approximation in cents: steps * (1200/edo) | ||
local approximation_cents = result.steps * (1200 / result.edo) | local approximation_cents = result.steps * (1200 / result.edo) | ||
local approx_str = string.format("%. | local approx_str = string.format("%.2f", approximation_cents) | ||
-- Python: abs_err = f"{result['abs_error']:+. | -- Python: abs_err = f"{result['abs_error']:+.2f}" | ||
local abs_err = format_error(result.abs_error) | local abs_err = format_error(result.abs_error) | ||
-- Python: rel_err = f"{result['rel_error']:+. | -- Python: rel_err = f"{result['rel_error']:+.2f}" | ||
local rel_err = format_error(result.rel_error) | local rel_err = format_error(result.rel_error) | ||