Module:Rational: Difference between revisions

Ganaram inukshuk (talk | contribs)
added is_within_int_limit function, because there were enough use-cases to add it
ArrowHead294 (talk | contribs)
mNo edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local seq = require("Module:Sequence")
local seq = require("Module:Sequence")
local utils = require("Module:Utils")
local utils = require("Module:Utils")
local p = {}


-- enter a numerator n and denominator m
-- enter a numerator n and denominator m
Line 332: Line 333:


-- compute a canonical representation of `a` modulo powers of `b`
-- compute a canonical representation of `a` modulo powers of `b`
-- TODO: describe the exact behavior
--      it seems bugged when the equave is a fraction
function p.modulo_mul(a, b)
function p.modulo_mul(a, b)
if type(a) == "number" then
if type(a) == "number" then
Line 659: Line 662:
local den = p.mul(p.add(k, 1), p.sub(k, 1))
local den = p.mul(p.add(k, 1), p.sub(k, 1))
return p.eq(a, p.div(p.pow(k, 2), den))
return p.eq(a, p.div(p.pow(k, 2), den))
end
-- check if an integer is prime
function p.is_prime(a)
if type(a) == "number" then
a = p.new(a)
end
-- nan, inf, zero, and negative numbers are not prime
if a.nan or a.inf or a.zero or a.sign < 0 then
return false
end
local flag = false -- flag for having exactly one prime factor
for factor, power in pairs(a) do
if type(factor) == "number" and power then
if flag or power ~= 1 then
return false
else
flag = true
end
end
end
return flag
end
end


Line 666: Line 693:
a = p.new(a)
a = p.new(a)
end
end
if a.nan or a.inf or a.zero then
return false
-- nan, inf, zero, and negative numbers are not highly composite
end
if a.nan or a.inf or a.zero or a.sign == -1 then
-- negative numbers are not highly composite
if a.sign == -1 then
return false
return false
end
end
-- non-integers are not highly composite
-- non-integers are not highly composite
for factor, power in pairs(a) do
for factor, power in pairs(a) do
Line 681: Line 707:
end
end
end
end
local last_power = 1 / 0
local last_power = 1 / 0
local max_prime = p.max_prime(a)
local max_prime = p.max_prime(a)
Line 1,508: Line 1,535:
function p.ket(frame)
function p.ket(frame)
local unparsed = frame.args[1] or "1"
local unparsed = frame.args[1] or "1"
local result = ""
local a = p.parse(unparsed)
local a = p.parse(unparsed)
if a == nil then
if a == nil then
return '<span style="color:red;">Invalid rational number: ' .. unparsed .. ".</span>"
result = '{{error|Invalid rational number: ' .. unparsed .. ".}}"
else
result = p.as_ket(a, frame)
end
end
return p.as_ket(a, frame)
return frame:preprocess(result)
end
end
p.monzo = p.ket
p.monzo = p.ket


return p
return p