Module:Simplified fraction: Difference between revisions
Jump to navigation
Jump to search
Tristanbay (talk | contribs) got rid of rational module stuff and replace it with separate function |
Tristanbay (talk | contribs) Maybe I need to initialize ft as empty string? |
||
| Line 18: | Line 18: | ||
local num, denom = simplify(num, denom) | local num, denom = simplify(num, denom) | ||
local whole = frame.args["mixed"] and math.floor(num / denom) or 0 | local whole = frame.args["mixed"] and math.floor(num / denom) or 0 | ||
local ft | local ft = "" | ||
if d == 1 then | if d == 1 then | ||
ft = string.format("%d", n) | ft = ft .. string.format("%d", n) | ||
elseif whole ~= 0 then | elseif whole ~= 0 then | ||
ft = string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom) | ft = ft .. string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom) | ||
else | else | ||
ft = string.format("{{frac|%d|%d}}", num, denom) | ft = ft .. string.format("{{frac|%d|%d}}", num, denom) | ||
end | end | ||
return frame:preprocess(ft) | return frame:preprocess(ft) | ||
end | end | ||
Revision as of 08:49, 12 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(n / b)
end
function p.simfrac(frame)
local n = frame.args["n"]
local d = frame.args["d"]
local num, denom = simplify(num, denom)
local whole = frame.args["mixed"] and math.floor(num / denom) or 0
local ft = ""
if d == 1 then
ft = ft .. string.format("%d", n)
elseif whole ~= 0 then
ft = ft .. string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom)
else
ft = ft .. string.format("{{frac|%d|%d}}", num, denom)
end
return frame:preprocess(ft)
end