Module:JI ratio finder: Difference between revisions

Ganaram inukshuk (talk | contribs)
Added option to add error to text output
Ganaram inukshuk (talk | contribs)
m Formatting; testing
Line 219: Line 219:
-- Default delimiter is a comma followed by a space
-- Default delimiter is a comma followed by a space
function p.ratios_to_text(ratios, delimiter)
function p.ratios_to_text(ratios, delimiter)
local ratios = ratios or { rat.new(5, 4), rat.new (81, 64) }
local ratios = ratios or { rat.new(5, 4), rat.new(81, 64), rat.new(9, 7) }
local delimiter = delimiter or ", "
local delimiter = delimiter or ", "


Line 235: Line 235:
-- Default delimiter is a comma followed by a space
-- Default delimiter is a comma followed by a space
function p.ratios_to_text_with_error(ratios, target_cents, delimiter, add_links)
function p.ratios_to_text_with_error(ratios, target_cents, delimiter, add_links)
local ratios = ratios or { rat.new(5, 4), rat.new (81, 64) }
local ratios = ratios or { rat.new(5, 4), rat.new(81, 64), rat.new(9, 7) }
local target_cents = target_cents
local target_cents = target_cents or 400
local delimiter = delimiter or ", "
local delimiter = delimiter or ", "
local add_links = add_links == true
local add_links = add_links == true
Line 243: Line 243:
for i = 1, #ratios do
for i = 1, #ratios do
local ratio_as_text  = rat.as_ratio(ratios[i])
local ratio_as_text  = rat.as_ratio(ratios[i])
local ratio_as_cents = rat.as_ratio(ratios[i])
local ratio_as_cents = rat.cents(ratios[i])
local diff = ratio_as_cents - target_cents
local diff = ratio_as_cents - target_cents
Line 253: Line 253:
if diff > 0 then
if diff > 0 then
text = text .. string.format(' (+%.3f)')
text = text .. string.format(' (+%.3f)', diff)
elseif diff < 0 then
elseif diff < 0 then
text = text .. string.format(' (%.3f)')
text = text .. string.format(' (%.3f)', diff)
elseif diff == 0 then
elseif diff == 0 then
text = text .. "(just)"
text = text .. "(just)"