Module:JI ratio finder: Difference between revisions
m Fixed logic for prime limit |
Added exact-ratio filtering |
||
| Line 161: | Line 161: | ||
return filtered_ratios | return filtered_ratios | ||
end | |||
-- Filters ratios by whether they exactly match a cent value | |||
-- Nonmatching ratios are filtered out; intended for filtering out all but the | |||
-- unison and equave. For best results, convert the unison and equave into cent | |||
-- values from their respective ratios. | |||
-- There are test cases that may fail: | |||
-- - Calculating the cent value of 27/8 and dividing by 3; this should be the | |||
-- the same as 3/2, but due to floating-point errors, this isn't the case. | |||
function p.filter_exact_ratio(ratios, cents) | |||
local ratios = ratios or { rat.new(3, 2), rat.new(40, 27), rat.new(32, 21) } | |||
local cents = cents or rat.cents(rat.new(27, 8)) / 3 | |||
local exact_ratio = nil | |||
for i = 1, #ratios do | |||
if rat.cents(ratios[i]) == cents then | |||
exact_ratio = rat.copy(ratios[i]) | |||
end | |||
end | |||
return exact_ratio | |||
end | end | ||