Module:Simplified fraction: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Tristanbay (talk | contribs)
Created reduced fraction module
 
ArrowHead294 (talk | contribs)
mNo edit summary
Line 3: Line 3:


function p.refrac(frame)
function p.refrac(frame)
local num, denom = rat.as_pair(rat.new(frame.args["n"], frame.args["d"])) -- hopefully this reduces the numbers
local n = frame.args["n"]
local d = frame.args["d"]
local num, denom = rat.as_pair(rat.new(n, d)) -- hopefully this reduces the numbers
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 frac = whole ~= 0 and string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom) or string.format("{{frac|%d|%d}}", num, denom)
local ft = (whole ~= 0 and string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom) or string.format("{{frac|%d|%d}}", num, denom))
return frame:preprocess(frac)
return frame:preprocess(ft)
end
end

Revision as of 02:02, 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
4 refrac (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
rat Module:Rational as_pair
new

No function descriptions were provided. The Lua code may have further information.


local p = {}
local rat = require("Module:Rational")

function p.refrac(frame)
	local n = frame.args["n"]
	local d = frame.args["d"]
	local num, denom = rat.as_pair(rat.new(n, d)) -- hopefully this reduces the numbers
	local whole = frame.args["mixed"] and math.floor(num / denom) or 0
	local ft = (whole ~= 0 and string.format("{{frac|%d|%d|%d}}", whole, num - whole * denom, denom) or string.format("{{frac|%d|%d}}", num, denom))
	return frame:preprocess(ft)
end