Module:Rational: Difference between revisions

gcd no longer needs to be defined here
Re-comment some functions; implement log base 2 from module: Utils
Line 877: Line 877:
end
end


-- find max prime involved in the factorisation of a rational number
-- find max prime involved in the factorisation
-- (a.k.a. prime limit or harmonic class) of a rational number
function p.max_prime(a)
function p.max_prime(a)
if type(a) == 'number' then
if type(a) == 'number' then
Line 896: Line 897:
end
end


-- compute log2 of a rational number
-- convert a rational number to its size in octaves
-- equal to log2 of the rational number
function p.log(a, base)
function p.log(a, base)
base = base or 2
base = base or 2
Line 917: Line 919:
for factor, power in pairs(a) do
for factor, power in pairs(a) do
if type(factor) == 'number' then
if type(factor) == 'number' then
logarithm = logarithm + power * math.log(factor) / math.log(base)
logarithm = logarithm + power * u._log(factor, base)
end
end
end
end
Line 1,074: Line 1,076:
for factor, power in pairs(a) do
for factor, power in pairs(a) do
if type(factor) == 'number' then
if type(factor) == 'number' then
h = h + math.abs(power) * math.log(factor) / math.log(2)
h = h + math.abs(power) * u._log(factor, 2)
end
end
end
end
Line 1,092: Line 1,094:
for factor, power in pairs(a) do
for factor, power in pairs(a) do
if type(factor) == 'number' then
if type(factor) == 'number' then
h2 = h2 + power * math.log (factor) / math.log(2)
h2 = h2 + power * u._log(factor, 2)
end
end
end
end
Line 1,193: Line 1,195:
end
end


-- convert a rational number to cents
-- convert a rational number to its size in cents
function p.cents(a)
function p.cents(a)
if type(a) == 'number' then
if type(a) == 'number' then
Line 1,204: Line 1,206:
for factor, power in pairs(a) do
for factor, power in pairs(a) do
if type(factor) == 'number' then
if type(factor) == 'number' then
c = c + power * math.log(factor)
c = c + power * u._log(factor, base)
end
end
end
end
return c * 1200 / math.log(2)
return c * 1200
end
end