Module:Rational: Difference between revisions

m You wouldn't need overflow protection if it could do m * n
Tag: Undo
All the special cases in as_ket make no sense
Line 1,366: Line 1,366:
end
end


-- return a rational number in ket notation
-- return a string of a rational number in monzo notation
-- NaN, infinity, zero values use special representations
-- calling Template: Monzo
function p.as_ket(a, frame, skip_many_zeros, only_numbers)
function p.as_ket(a, frame, skip_many_zeros, only_numbers)
if skip_many_zeros == nil then
if skip_many_zeros == nil then
Line 1,376: Line 1,376:
a = p.new(a)
a = p.new(a)
end
end
-- special case: NaN
if a.nan then
-- special cases
return "NaN"
if a.nan or a.inf or a.zero or a.sign < 0 then
return "n/a"
end
end
-- special case: infinity
if a.inf then
-- regular case: positive finite ratio
local sign = "+"
local s = ""
if a.sign < 0 then
sign = "-"
end
return sign .. "∞"
end
-- special case: zero
if a.zero then
return "0"
end
-- regular case: not NaN, not infinity, not zero


local s = ""
if not only_numbers and a.sign < 0 then
s = s .. "-"
end
-- preparing the argument
-- preparing the argument
local max_prime = p.max_prime(a)
local max_prime = p.max_prime(a)