Module:Simplified fraction: Difference between revisions
Jump to navigation
Jump to search
Tristanbay (talk | contribs) seeing if this fixes the nil value returned |
Tristanbay (talk | contribs) Undo revision 214299 by Tristanbay (talk) Tag: Undo |
||
| (13 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p. | local function simplify(n, d) | ||
local a = math.max(n, d) | |||
local b = math.min(n, d) | |||
local g = 1 | |||
while b ~= 0 do | |||
g = a % b | |||
a = b | |||
b = g | |||
end | |||
return math.floor(n / a), math.floor(d / a) | |||
end | |||
function p.simfrac(frame) | |||
local n = frame.args["n"] | local n = frame.args["n"] | ||
local d = frame.args["d"] | local d = frame.args["d"] | ||
local num, denom = | local mixed = frame.args["mixed"] == "true" and true or false | ||
local whole = | local num, denom = simplify(n, d) | ||
local ft = (whole ~= 0 | local whole = mixed and math.floor(num / denom) or 0 | ||
local ft | |||
if denom == 1 then | |||
ft = string.format("%d", num) | |||
elseif whole ~= 0 then | |||
ft = string.format("%d %d⁄%d", whole, num - whole * denom, denom) | |||
else | |||
ft = string.format("%d⁄%d", num, denom) | |||
end | |||
return frame:preprocess(ft) | return frame:preprocess(ft) | ||
end | end | ||
return p | |||
Latest revision as of 00:52, 24 October 2025
- This module implements a template that is currently missing or does not use this module. (edit template)
This module automatically creates a fraction in simplest form from a numerator and denominator. Whether the fraction is in a mixed form or not when greater than 1 can be controlled using the mixed parameter.
| Introspection summary for Module:Simplified fraction | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
No function descriptions were provided. The Lua code may have further information.
local p = {}
local function simplify(n, d)
local a = math.max(n, d)
local b = math.min(n, d)
local g = 1
while b ~= 0 do
g = a % b
a = b
b = g
end
return math.floor(n / a), math.floor(d / a)
end
function p.simfrac(frame)
local n = frame.args["n"]
local d = frame.args["d"]
local mixed = frame.args["mixed"] == "true" and true or false
local num, denom = simplify(n, d)
local whole = mixed and math.floor(num / denom) or 0
local ft
if denom == 1 then
ft = string.format("%d", num)
elseif whole ~= 0 then
ft = string.format("%d %d⁄%d", whole, num - whole * denom, denom)
else
ft = string.format("%d⁄%d", num, denom)
end
return frame:preprocess(ft)
end
return p