Module:Math: Difference between revisions
m>Dragons flight No edit summary |
m>Dragons flight No edit summary |
||
Line 26: | Line 26: | ||
local decimal = string.find( x, '.', 1, true ) | local decimal = string.find( x, '.', 1, true ) | ||
local | local exponent_pos = string.find( x, 'E', 1, true ) | ||
local result = 0; | local result = 0; | ||
if | if exponent_pos ~= nil then | ||
exponent = string.sub( x, | local exponent = string.sub( x, exponent_pos + 1 ) | ||
x = string.sub( x, 1, | x = string.sub( x, 1, exponent_pos - 1 ) | ||
result = result - tonumber( exponent ) | result = result - tonumber( exponent ) | ||
end | end |
Revision as of 07:57, 21 February 2013
Documentation for this module may be created at Module:Math/doc
local z = {} function z.random( frame ) first = tonumber(frame.args[1]) -- if it doesn't exist it's NaN, if not a number it's nil second = tonumber(frame.args[2]) if first then -- if NaN or nil, will skip down to final return if first <= second then -- could match if both nil, but already checked that first is a number in last line return math.random(first, second) end return math.random(first) end return math.random() end function z.precision( frame ) return z._precision( frame.args[1] or frame.args.x or '0' ) end -- Determines precision of a number using the string representation function z._precision( x ) x = string.upper( x ) -- Remove leading / trailing whitespace x = x:match "^%s*(.-)%s*$"; local decimal = string.find( x, '.', 1, true ) local exponent_pos = string.find( x, 'E', 1, true ) local result = 0; if exponent_pos ~= nil then local exponent = string.sub( x, exponent_pos + 1 ) x = string.sub( x, 1, exponent_pos - 1 ) result = result - tonumber( exponent ) end if decimal ~= nil then result = result + string.len( x ) - decimal return result end local pos = string.len( x ); while x:byte(pos) == string.byte('0') do pos = pos - 1 result = result - 1 if pos <= 0 then return 0 end end return result end return z