Module:Simplified fraction: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Tristanbay (talk | contribs)
more stupid mistake fixing, also testing
Tristanbay (talk | contribs)
fixing the stupidest mistake of all?
Line 14: Line 14:


function p.simfrac(frame)
function p.simfrac(frame)
local n = 14
local n = frame.args["n"]
local d = 9
local d = frame.args["d"]
local num, denom = simplify(n, d)
local num, denom = simplify(n, d)
local whole = true and math.floor(num / denom) or 0
local whole = frame.args["mixed"] and math.floor(num / denom) or 0
local ft
local ft
if denom == 1 then
if denom == 1 then
Line 28: Line 28:
return frame:preprocess(ft)
return frame:preprocess(ft)
end
end
return p

Revision as of 09:12, 12 October 2025

Module documentation[view] [edit] [history] [purge]
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 
Functions provided (1)
Line Function Params
15 simfrac (invokable) (frame)
Lua modules required (0)
Variable Module Functions used

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 num, denom = simplify(n, d)
	local whole = frame.args["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("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom)
	else
		ft = string.format("{{frac|%d|%d}}", num, denom)
	end
	return frame:preprocess(ft)
end

return p