Module:Rational: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
m Code reorganised
Line 324: Line 324:
end
end


-- a version of as_ket() that can be {{#invoke:}}d
-- parse a rational number
function p.ket(frame)
-- returns nil on failure
local unparsed = frame.args[1] or '1'
function p.parse(unparsed)
-- rational form
-- rational form
local sign, n, _m, m = unparsed:match('(%-?)%s*(%d+)%s*(/%s*(%d+))')
local sign, n, _m, m = unparsed:match('(%-?)%s*(%d+)%s*(/%s*(%d+))')
Line 334: Line 334:
if n == nil then
if n == nil then
-- parsing failure
-- parsing failure
return '<span style="color:red;">Invalid rational number: ' .. unparsed .. '.</span>'
return nil
else
else
m = 1
m = 1
Line 349: Line 349:
end
end
end
end
a = p.new(n, m)
return p.new(n, m)
end
 
-- a version of as_ket() that can be {{#invoke:}}d
function p.ket(frame)
local unparsed = frame.args[1] or '1'
local a = p.parse(unparsed)
if a == nil then
return '<span style="color:red;">Invalid rational number: ' .. unparsed .. '.</span>'
end
return p.as_ket(a, frame)
return p.as_ket(a, frame)
end
end


return p
return p