Anonymous user
Module:Convert: Difference between revisions
some overflow checks; fix ntsh to match Jimp's changes to Template:Ntsh in Sep 2012
(refactor and implement disp=table/tablecen) |
(some overflow checks; fix ntsh to match Jimp's changes to Template:Ntsh in Sep 2012) |
||
Line 3:
local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)
local
local floor = math.floor
local format = string.format
local log10 = math.log10
Line 90 ⟶ 91:
cvt_bug_convert = { 'Bug: Cannot convert between specified units', 'general' },
cvt_empty_option = { 'Ignored empty option "%s"', 'option', warning = true },
cvt_invalid_num = { 'Number is too large or too small', 'general' },
cvt_mismatch = { 'Cannot convert "%s" to "%s"', 'mismatch' },
cvt_no_default = { 'Unit "%s" has no default output unit', 'unknown' },
Line 441 ⟶ 443:
end
local function
-- Return
-- Expressed as a string, overflow or other problems are indicated with
-- text like "1.#INF" or ".#IND" which are regarded as invalid here.
if type(num) == 'number' and tostring(num):find('#', 1, true) == nil then
lenblock = 12▼
end▼
end
end
local function ntsh(
-- Return html text to be used for a hidden sort key so that
-- the given number will be sorted in numeric order.
-- If debug == 'yes', output is in a box (not hidden).
-- This implements Template:Ntsh (number table sorting, hidden).
local result
if
if
result = '
else
end
elseif num == 0 then
result = '5000000000000000000'
else
if num > 0
else
end
result = format('%d', prefix) .. format('%015.0f', floor(num * 10^(14-mag)))
end
if debug == 'yes' then
Line 765 ⟶ 758:
numstr = change_sign(numstr)
end
if
return nil -- overflow or similar
end
Line 847 ⟶ 840:
end
singular = false -- any fraction (even with value 1) is regarded as plural
if not valid_number(value) then -- for example, "1e310"
return false, { 'cvt_invalid_num' }
end
if show == nil then
Line 874 ⟶ 870:
-- Return true, n where n = integer equivalent to given text,
-- or return false, t where t is an error message table.
-- Input should be the text for a simple integer (no separators, no Unicode minus
-- limited size). Using regex avoids irritations with input like '-0.000001'.
if text == nil then return false, { 'cvt_no_num' } end
if #text > 9 or text:match('^-?%d+$') == nil then
return false, { invalid, text }
end
Line 923 ⟶ 919:
return nil
end
end
preunit2 = preunit2 or ''
Line 1,351 ⟶ 1,347:
-- Calculate minimum precision from absolute value.
adjust = 0
local kelvin =
if kelvin < 1e-8 then -- assume nonzero due to input or calculation precision problem
minprec = 2
Line 1,367 ⟶ 1,363:
adjust = -log10(in_current.scale)
else
adjust = log10(
end
adjust = adjust + log10(2)
Line 1,510 ⟶ 1,506:
inclean = extra.inclean or inclean
outvalue = extra.outvalue
end
if not valid_number(outvalue) then
return false, { 'cvt_invalid_num' }
end
local isnegative
Line 1,862 ⟶ 1,861:
end
if value then -- some unusual units do not always set value field
value =
singular = (0 < value and value < 1.0001)
end
|