Module:Rational: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
mNo edit summary
Line 746: Line 746:
end
end


-- FJS representation of a rational number
-- might be a bit incorrect
function p.as_FJS(a)
function p.as_FJS(a)
if type(a) == 'number' then
if type(a) == 'number' then
Line 774: Line 776:
table.sort(utonal)
table.sort(utonal)
local fifths = b[3] or 0
local fifths = b[3] or 0
mw.log(p.as_ratio(b))
mw.log('fifths=' .. fifths)
if fifths % 7 == 6 then
b = p.div(b, red(p.new(2, 3)))
else
b = p.div(b, red(p.pow(p.new(3, 2), fifths % 7)))
end


local red3 = red(p.pow(3, fifths))
b = p.div(b, p.pow(p.new(2187, 2048), math.floor((fifths + 1) / 7)))
mw.log('dividing by ' .. p.as_ratio(red3))
b = p.div(b, red3)
local octaves = b[2] or 0
local octaves = b[2] or 0
mw.log('octaves=' .. octaves)
if math.abs(fifths) >= 12 and math.abs(fifths) % 7 == 5 then
-- still wrong results with 4096/2187: d1 instead of d8
octaves = octaves - u.signum(fifths)
mw.log('octaves=' .. octaves)
end
-- a terrible hack, I have no idea how/why this is supposed to work
-- local octaves = (b[2] or 0) + fifths + 4 * math.floor(fifths / 7) + math.floor((fifths % 7) / 2)
local noshift = {1, 5, 2, 6, 3, 7, 4}
local noshift = {1, 5, 2, 6, 3, 7, 4}
local num = noshift[1 + (fifths % 7)] + octaves * 7
local base_num = noshift[1 + (fifths % 7)]
local num = base_num + octaves * 7
if octaves < 0 then
if octaves < 0 then
mw.log('octaves < 0, inverting')
fifths = -fifths
fifths = -fifths
-- wrong results on 3^53/2^84, which should be AAAAAAA3 but is computed as ddddddd-6
octaves = -octaves - 1
-- octaves = -(b[2] or 0) + fifths + 4 * math.floor(fifths / 7) + math.floor((fifths % 7) / 2)
base_num = (8 - base_num) % 7 + 1
-- num = -noshift[1 + (fifths % 7)] - octaves * 7
num = -base_num - octaves * 7
if fifths ~= 0 then
num = num - 2
else
num = num + 5
end
end
end