Module:Convert: Difference between revisions

From TEPwiki, Urth's Encyclopedia
Jump to navigation Jump to search
Content added Content deleted
(rewrite rounding code to follow procedures used in old template; refactor)
(fixes to rounding, including handling temperature rounding rules)
Line 125: Line 125:
last = #text
last = #text
else
else
last = last - 1
last = last - 1 -- index of last character before dot/e/E
end
end
if last >= 4 then
if last >= 4 then
Line 193: Line 193:
end
end


local function format_number(show, exponent)
local function format_number(show, exponent, isnegative)
-- Return wikitext to display the value implied by the arguments:
-- Return wikitext to display the value implied by the arguments:
-- exponent is nil; and
-- exponent is nil; and
Line 201: Line 201:
-- show is a string of digits (no sign and no dot; there is an
-- show is a string of digits (no sign and no dot; there is an
-- implied dot before show; show does not start with '0').
-- implied dot before show; show does not start with '0').
-- The result has numsep inserted where necessary, and uses exponent
-- The result:
-- notation for very small or large values.
-- * Includes a Unicode minus if isnegative.
-- * Has numsep inserted where necessary.
-- The result has no more than config.maxsigfig significant digits
-- * Uses exponent notation for very small or large values.
-- (same as old template and {{#expr}}).
-- * Has no more than config.maxsigfig significant digits
-- (same as old template and {{#expr}}).
local sign
if isnegative then
sign = '−' -- Unicode minus
else
sign = ''
end
local numdot = config.numdot
local numdot = config.numdot
local maxlen = config.maxsigfig
local maxlen = config.maxsigfig
Line 237: Line 245:
if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
-- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
-- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
return with_exponent(show, exponent-1)
return sign .. with_exponent(show, exponent-1)
end
end
if exponent >= #show then
if exponent >= #show then
Line 247: Line 255:
end
end
end
end
if isnegative and show:match('^0.?0*$') then
return with_separator(show)
sign = '' -- don't show minus if result is negative but rounds to zero
end
return sign .. with_separator(show)
end
end


Line 465: Line 476:
-- Return a default value for precision (an integer like 2, 0, -2).
-- Return a default value for precision (an integer like 2, 0, -2).
-- Code follows procedures used in old template.
-- Code follows procedures used in old template.
-- Am putting exceptions to standard calculations here, as they are
-- discovered. Can later decide if something cleaner should be done.
-- LATER: The hand unit of length might need special processing.
-- LATER: The hand unit of length might need special processing.
local log10 = math.log10
local log10 = math.log10
local prec, minprec, adjust
local prec, minprec, adjust
local utype = parms.out_unit_table.utype
local in_unit_table = parms.in_unit_table
local out_unit_table = parms.out_unit_table
local utype = out_unit_table.utype
local fudge = 1e-14 -- {{Order of magnitude}} adds this, so we do too
-- Find fractional digits, handling cases like inclean = '12.345e6'.
local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
if utype == 'temperature' then
if utype == 'temperature' then
-- LATER: Give an error message if (invalue < in_unit_table.offset): below absolute zero?
-- Not just multiplication by a factor.
-- TODO Fix following.
adjust = 0
adjust = 0
local kelvin = (invalue - in_unit_table.offset) * in_unit_table.scale
minprec = 0 -- precision which would give 3 sigfigs in kelvin
if kelvin <= 0 then -- can get zero, or small but negative value due to precision problems
minprec = 2
else
minprec = 2 - math.floor(log10(kelvin) + fudge) -- 3 sigfigs in kelvin
end
else
else
if outvalue <= 0 then
if invalue == 0 or outvalue <= 0 then
-- We are never called with a negative outvalue, but it might be zero.
-- We are never called with a negative outvalue, but it might be zero.
-- This is special-cased to avoid a bad log10() call.
-- This is special-cased to avoid calculation exceptions.
return 0
return 0
end
end
if out_unit_table.symbol == 'ft' and dot == '' then
local factor
-- More precision when output ft with integer input value.
if invalue == 0 then
factor = 1 -- LATER is this correct?
adjust = -log10(in_unit_table.scale)
else
else
factor = math.abs(outvalue / invalue)
adjust = log10(math.abs(invalue / outvalue))
end
end
adjust = -log10(factor) + log10(2)
adjust = adjust + log10(2)
-- Ensure that the output has at least two significant figures.
-- Ensure that the output has at least two significant figures.
-- {{Order of magnitude}} adds a fudge, so we do too.
minprec = 1 - math.floor(log10(outvalue) + fudge)
minprec = 1 - math.floor(log10(outvalue) + 1e-14)
end
end
-- Find fractional digits, handling cases like inclean = '12.345e6'.
local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
if dot == '' then
if dot == '' then
prec = -integer:match('0*$'):len() -- '12300' gives -2, but so does '12300e-5'
prec = -integer:match('0*$'):len() -- '12300' gives -2, but so does '12300e-5'
else
else
if #fraction == 0 then
if #fraction == 0 and utype ~= 'temperature' then
prec = 1 -- "123." has same precision as "123.0", like old template
prec = 1 -- "123." has same precision as "123.0", like old template
else
else
Line 538: Line 557:
end
end
local outvalue = convert(invalue, parms.in_unit_table, parms.out_unit_table)
local outvalue = convert(invalue, parms.in_unit_table, parms.out_unit_table)
local propersign
local isnegative
if outvalue < 0 then
if outvalue < 0 then
propersign = '−' -- Unicode minus
isnegative = true
outvalue = -outvalue
outvalue = -outvalue
else
propersign = ''
end
end
local success
local success
Line 570: Line 587:
if precision then
if precision then
if precision >= 0 then
if precision >= 0 then
if precision <= 8 then
-- Add a fudge to handle common cases of bad rounding due to inability
-- to precisely represent some values. This makes the following work:
-- {{convert|-100.1|C|K}} and {{convert|5555000|um|m|2}}.
-- Old template uses #expr round, which invokes PHP round().
-- LATER: Investigate how PHP round() works.
outvalue = outvalue + 2e-14
end
local fmt = '%.' .. string.format('%d', precision) .. 'f'
local fmt = '%.' .. string.format('%d', precision) .. 'f'
show = string.format(fmt, outvalue)
show = string.format(fmt, outvalue)
Line 583: Line 608:
end
end
end
end
if (show == '1' or show:match('^1%.0*$') ~= nil) and propersign == '' then
if (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative then
-- Use match because on some systems 0.99999999999999999 is 1.0.
-- Use match because on some systems 0.99999999999999999 is 1.0.
singular = true
singular = true
end
end
return true, propersign .. format_number(show, exponent), singular
return true, format_number(show, exponent, isnegative), singular
end
end



Revision as of 04:29, 28 October 2012

Documentation for this module may be created at Module:Convert/doc

--[[
Later TODO Too many items to list, but following are some points:
- Some conversions require two outputs: {{convert|55|nmi|km mi}}.
- Some units have two values: {{convert|3.21|m|ftin}}.
- Some units use the plural name, not the symbol, example:
    {{convert|12|ha}} --should give--> 12 hectares (30 acres)
]]

-- Conversion data is defined in another module because it is too large
-- to be conveniently included here.
-- A testing program can set the global variable 'is_test_run'.
local convertdata = require(is_test_run and "convertdata" or "Module:Convertdata")
local units = convertdata.units
local defaultunits = convertdata.defaultunits

local function strip(text)
    -- If text is a string, return its content with no leading/trailing
    -- whitespace. Otherwise return nil.
    if type(text) == 'string' then
        return text:match("^%s*(.-)%s*$")
    end
end

-- Configuration options to keep magic values in one location.
local config = {}

local function get_config(frame)
    -- Return table of configuration options.
    local cfg = {
        -- Defaults that can be overridden by template.
        numdot = '.',       -- decimal mark before fractional digits
        numsep = ',',       -- thousands separator for numbers (',', '.', or nil)
        maxsigfig = 14,     -- maximum number of significant figures
    }
    for k, v in frame:argumentPairs() do
        cfg[k] = v          -- arguments from template's {{#invoke:}}
    end
    if cfg.maxsigfig > 20 then
        cfg.maxsigfig = 20  -- limit abuse
    end
    return cfg
end

local function ntsh_complement(text)
    -- Return text (string of digits) after subtracting each digit from 9.
    local result = ''
    local first, last = 1, #text
    while first <= last do
        local lenblock = last + 1 - first
        if lenblock > 12 then
            lenblock = 12
        end
        local block = tonumber(text:sub(first, first + lenblock - 1))
        local nines = tonumber(string.rep('9', lenblock))
        local fmt = '%0' .. tostring(lenblock) .. '.0f'
        result = result .. fmt:format(nines - block)
        first = first + lenblock
    end
    return result
end

local function ntsh(n, debug)
    -- 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, i, f, style
    if n >= 0 then
        if n > 1e16 then
            result = '~'
        else
            i, f = math.modf(n)
            f = math.floor(1e6 * f)
            result = string.format('&1%016.0f%06d', i, f)
        end
    else
        n = -n
        if n > 1e16 then
            result = '!'
        else
            i, f = math.modf(n)
            f = math.floor(1e6 * f)
            result = string.format('%016.0f%06d', i, f)
            result = '&0' .. ntsh_complement(result)
        end
    end
    if debug == 'yes' then
        style = 'border:1px solid'
    else
        style = 'display:none'
    end
    return '<span style="' .. style .. '">' .. result .. '</span>'
end

local function change_sign(text)
    -- Change sign of text for correct appearance because it is negated.
    if text:sub(1, 1) == '-' then
        return text:sub(2)
    end
    return '-' .. text
end

local function use_minus(text)
    -- Return text with Unicode minus instead of '-', if present.
    if text:sub(1, 1) == '-' then
        return '−' .. text:sub(2)
    end
    return text
end

local function with_separator(text)
    -- Return text with thousand separators inserted.
    -- The given text is like '123' or '12345.6789' or '1.23e45'
    -- (e notation can only occur when processing an input value).
    -- The text has no sign (caller inserts that later, if necessary).
    -- Separator is inserted only in the integer part of the significand
    -- (not after numdot, and not after 'e' or 'E').
    -- Four-digit integer parts have a separator (like '1,234').
    local numsep = config.numsep
    if numsep == '' then
        return text
    end
    local last = text:match('()[' .. config.numdot .. 'eE]')  -- () returns position
    if last == nil then
        last = #text
    else
        last = last - 1  -- index of last character before dot/e/E
    end
    if last >= 4 then
        local groups = {}
        local first = last % 3
        if first > 0 then
            table.insert(groups, text:sub(1, first))
        end
        first = first + 1
        while first < last do
            table.insert(groups, text:sub(first, first+2))
            first = first + 3
        end
        return table.concat(groups, numsep) .. text:sub(last+1)
    end
    return text
end

-- Input values can use values like 1.23e12, but are never displayed
-- using exponent notation like 1.23×10¹².
-- Very small or very large output values use exponent notation.
-- Use fmtpower:format(significand, exponent) where each arg is a string.
local fmtpower = '%s<span style="margin-left:0.2em">×<span style="margin-left:0.1em">10</span></span><sup>%s</sup>'

local function with_exponent(show, exponent)
    -- Return wikitext to display the implied value in exponent notation.
    if #show > 1 then
        show = show:sub(1, 1) .. config.numdot .. show:sub(2)
    end
    return fmtpower:format(show, use_minus(tostring(exponent)))
end

local function make_sigfig(value, sigfig)
    -- Return show, exponent that are equivalent to the result of
    -- converting the number 'value' (where value >= 0) to a string,
    -- rounded to 'sigfig' significant figures.
    -- The returned items are:
    --   show: a string of digits; no sign and no dot;
    --         there is an implied dot before show.
    --   exponent: a number (an integer) to shift the implied dot.
    -- Resulting value = tonumber('.' .. show) * 10^exponent.
    -- Examples:
    --   make_sigfig(23.456, 3) returns '235', 2 (.235 * 10^2).
    --   make_sigfig(0.0023456, 3) returns '235', -2 (.235 * 10^-2).
    --   make_sigfig(0, 3) returns '000', 1 (.000 * 10^1).
    if sigfig <= 0 then
        sigfig = 1
    elseif sigfig > config.maxsigfig then
        sigfig = config.maxsigfig
    end
    if value == 0 then
        return string.rep('0', sigfig), 1
    end
    local exp, frac = math.modf(math.log10(value))
    if frac >= 0 then
        frac = frac - 1
        exp = exp + 1
    end
    local digits = tostring(math.floor((10^(frac + sigfig)) + 0.5))
    if #digits > sigfig then
        -- Overflow (for sigfig=3: like 0.9999 rounding to "1000"; need "100").
        digits = digits:sub(1, sigfig)
        exp = exp + 1
    end
    assert(#digits == sigfig, 'Bug: rounded number has wrong length')
    return digits, exp
end

local function format_number(show, exponent, isnegative)
    -- Return wikitext to display the value implied by the arguments:
    --   exponent is nil; and
    --   show is a string of digits (no sign), with an optional dot;
    -- or:
    --   exponent is a number (integer) indicating where dot should be;
    --   show is a string of digits (no sign and no dot; there is an
    --        implied dot before show; show does not start with '0').
    -- The result:
    -- * Includes a Unicode minus if isnegative.
    -- * Has numsep inserted where necessary.
    -- * Uses exponent notation for very small or large values.
    -- * Has no more than config.maxsigfig significant digits
    --   (same as old template and {{#expr}}).
    local sign
    if isnegative then
        sign = '−'  -- Unicode minus
    else
        sign = ''
    end
    local numdot = config.numdot
    local maxlen = config.maxsigfig
    if exponent == nil then
        local integer, dot, fraction = show:match('^(%d*)([' .. numdot .. ']?)(.*)')
        if #integer >= 10 then
            show = integer .. fraction
            exponent = #integer
        elseif integer == '0' or integer == '' then
            local zeros, figs = fraction:match('^(0*)([^0]?.*)')
            if #figs == 0 then
                if #zeros > maxlen then
                    show = '0' .. numdot .. zeros:sub(1, maxlen)
                end
            elseif #zeros >= 4 then
                show = figs
                exponent = -#zeros
            elseif #figs > maxlen then
                show = '0' .. numdot .. zeros .. figs:sub(1, maxlen)
            end
        else
            maxlen = maxlen + #dot
            if #show > maxlen then
                show = show:sub(1, maxlen)
            end
        end
    end
    if exponent ~= nil then
        if #show > maxlen then
            show = show:sub(1, maxlen)
        end
        if exponent > 10 or exponent <= -4 or (exponent == 10 and show ~= '1000000000') then
            -- Rounded value satisfies: value >= 1e9 or value < 1e-4 (1e9 = 0.1e10).
            return sign .. with_exponent(show, exponent-1)
        end
        if exponent >= #show then
            show = show .. string.rep('0', exponent - #show)  -- result has no dot
        elseif exponent <= 0 then
            show = '0' .. numdot .. string.rep('0', -exponent) .. show
        else
            show = show:sub(1, exponent) .. numdot .. show:sub(exponent+1)
        end
    end
    if isnegative and show:match('^0.?0*$') then
        sign = ''  -- don't show minus if result is negative but rounds to zero
    end
    return sign .. with_separator(show)
end

-- Fraction output format (like old template).
-- frac1: sign, numerator, denominator
-- frac2: wholenumber, sign, numerator, denominator
local frac1 = '<span style="white-space:nowrap">%s<sup>%s</sup>&frasl;<sub>%s</sub></span>'
local frac2 = '<span class="frac nowrap">%s<s style="display:none">%s</s><sup>%s</sup>&frasl;<sub>%s</sub></span>'

local function extract_fraction(text, negative)
    -- If text represents a fraction, return value, show where
    -- value is a number and show is a string.
    -- Otherwise, return nil.
    --
    -- In the following, '(3/8)' represents the wikitext required to
    -- display a fraction with numerator 3 and denominator 8.
    -- In the wikitext, Unicode minus is used for a negative value.
    --   text          value, show            value, show
    --                 if not negative       if negative
    --   3 / 8         0.375, '(3/8)'        -0.375, '−(3/8)'
    --   2 + 3 / 8     2.375, '2(3/8)'       -1.625, '−2(−3/8)'
    --   2 - 3 / 8     1.625, '2(−3/8)'      -2.375, '−2(3/8)'
    --   1 + 20/8      3.5  , '1/(20/8)'     1.5   , '−1/(−20/8)'
    --   1 - 20/8      -1.5., '1(−20/8)'     -3.5  , '−1(20/8)'
    -- Wherever an integer appears above, numbers like 1.25 or 12.5e-3
    -- (which may be negative) are also accepted (like old template).
    -- Template interprets '1.23e+2+12/24' as '123(12/24)' = 123.5!
    local lhs, negfrac, rhs, numstr, numerator, denstr, denominator, wholestr, whole, value
    lhs, denstr = text:match('^%s*([^/]-)%s*/%s*(.-)%s*$')
    denominator = tonumber(denstr)
    if denominator == nil then return nil end
    wholestr, negfrac, rhs = lhs:match('^%s*(.-[^eE])%s*([+-])%s*(.-)%s*$')
    if wholestr == nil or wholestr == '' then
        wholestr = nil
        whole = 0
        numstr = lhs
    else
        whole = tonumber(wholestr)
        if whole == nil then return nil end
        numstr = rhs
    end
    negfrac = (negfrac == '-')
    numerator = tonumber(numstr)
    if numerator == nil then return nil end
    if negative == negfrac or wholestr == nil then
        value = whole + numerator / denominator
    else
        value = whole - numerator / denominator
        numstr = change_sign(numstr)
    end
    if tostring(value):find('#', 1, true) then
        return nil  -- overflow or similar
    end
    numstr = use_minus(numstr)
    denstr = use_minus(denstr)
    local wikitext
    if wholestr then
        local sign = negative and '−' or '+'
        if negative then
            wholestr = change_sign(wholestr)
        end
        wikitext = frac2:format(use_minus(wholestr), sign, numstr, denstr)
    else
        local sign = negative and '−' or ''
        wikitext = frac1:format(sign, numstr, denstr)
    end
    return value, wikitext
end

local missing = { 'Need value', 'Need second value' }
local invalid = { 'Value "%s" must be a number', 'Second value "%s" must be a number' }

local function extract_number(args, index, which)
    -- Return true if successfully extract a number from the text in args[index].
    -- Otherwise, return false, message.
    -- Parameter 'which' (1 or 2) selects which input value is being processed.
    -- Before processing, the input text is cleaned:
    -- * Any thousand separators (valid or not) are removed.
    -- * Any sign (and optional following whitespace) is replaced with
    --   '-' (if negative) or '' (otherwise).
    --   That replaces Unicode minus with '-'.
    -- If successful, following elements in table args are updated
    -- (first row applies if which == 1, second otherwise):
    --    args.in_value1,  args.in_singular1,  args.in_clean1,  args.in_show1
    --    args.in_value2,  args.in_singular2,  args.in_clean2,  args.in_show2
    -- Value is a valid number.
    -- Singular is true if value is 1 (singular form of units will be used).
    -- Singular is false if value is -1 (like old template).
    -- Clean is cleaned text with any separators and sign removed.
    -- Show is text formatted for output:
    -- * Thousand separators are inserted.
    -- * If negative, a Unicode minus is used; otherwise the sign
    --   is '+' (if the input text used '+'), or is ''.
    -- TODO Think about fact that the input value might be like 1.23e+123.
    -- Will the exponent break anything?
    local text = strip(args[index])
    if text == nil or text == '' then return false, missing[which] end
    local clean, sign
    local numsep = config.numsep
    if numsep == '' then
        clean = text
    else
        clean = text:gsub('[' .. numsep .. ']', '')  -- use '[.]' if numsep is '.'
    end
    -- Remove any sign character (assuming a number starts with '.' or a digit).
    sign, clean = clean:match('^%s*([^ .%d]*)%s*(.*)')
    if sign == nil or clean == nil then
        return false, missing[which]  -- should never occur
    end
    local propersign, negative
    if sign == '−' or sign == '-' then
        propersign = '−'  -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)
        negative = true
    elseif sign == '+' then
        propersign = '+'
        negative = false
    elseif sign == '' then
        propersign = ''
        negative = false
    else
        return false, (invalid[which]):format(text)
    end
    local show, singular
    local value = tonumber(clean)
    if value == nil then
        value, show = extract_fraction(clean, negative)
        if value == nil then
            return false, (invalid[which]):format(text)
        end
        singular = false  -- any fraction (even with value 1) is regarded as plural
    end
    if show == nil then
        singular = (value == 1)
        show = propersign .. with_separator(clean)
    end
    if negative and (value ~= 0) then
        value = -value
    end
    if which == 1 then
        args.in_value1 = value
        args.in_singular1 = singular
        args.in_clean1 = clean
        args.in_show1 = show
    else
        args.in_value2 = value
        args.in_singular2 = singular
        args.in_clean2 = clean
        args.in_show2 = show
    end
    return true
end

local function require_integer(text, missing, invalid)
    -- Return true, n where n = integer equivalent to given text (or false, message).
    -- Input should be the text for a simple integer (no separators, no Unicode minus).
    -- Using regex avoids irritations with input like '-0.000001'.
    if text == nil then return false, missing end
    if string.match(text, '^-?%d+$') == nil then
        return false, invalid:format(text)
    end
    return true, tonumber(text)
end

local function get_parms(pframe)
    -- Return true, t where t is a table with all arguments passed to the
    -- template converted to named arguments, or return false, message.
    -- Except for range, which is nil or a table, the named args that are
    -- added here could be provided by the user of the template.
    -- MediaWiki removes leading and trailing whitespace from the values of
    -- named arguments. However, the values of numbered arguments include any
    -- whitespace entered in the template, and whitespace is used by some
    -- parameters (example: the numbered parameters associated with "disp=x").
    local range_types = {  -- text to separate input, output ranges
        ['and']   = {' and ', '&nbsp;and '},
        ['by']    = {' by ', '&nbsp;by '},
        ['to']    = {' to ', '&nbsp;to '},
        ['-']     = {'–', '–'},
        ['to(-)'] = {'&nbsp;to ', '–'},
        ['x']     = {' by ', '&nbsp;×&nbsp;'},
        ['+/-']   = {'&nbsp;±&nbsp;', '&nbsp;±&nbsp;'},
    }
    local success, msg
    local args = {}  -- arguments passed to template
    for k, v in pframe:argumentPairs() do
        args[k] = v
    end
    success, msg = extract_number(args, 1, 1)
    if not success then return success, msg end
    local in_unit, out_unit, precision
    local next = strip(args[2])
    local i = 3
    local range = range_types[next]
    if range == nil then
        in_unit = next
    else
        success, msg = extract_number(args, 3, 2)
        if not success then return success, msg end
        in_unit = strip(args[4])
        i = 5
    end
    next = strip(args[i])
    if tonumber(next) == nil then
        out_unit = next
        precision = strip(args[i+1])
    else
        precision = next
    end
    if in_unit == nil then return false, 'Need input unit' end
    args.in_unit = in_unit
    args.out_unit = out_unit
    args.range = range
    args.precision = args.precision or precision  -- allow named parameter
    return true, args
end

local function default_precision(inclean, invalue, outvalue, parms)
    -- Return a default value for precision (an integer like 2, 0, -2).
    -- Code follows procedures used in old template.
    -- Am putting exceptions to standard calculations here, as they are
    -- discovered. Can later decide if something cleaner should be done.
    -- LATER: The hand unit of length might need special processing.
    local log10 = math.log10
    local prec, minprec, adjust
    local in_unit_table = parms.in_unit_table
    local out_unit_table = parms.out_unit_table
    local utype = out_unit_table.utype
    local fudge = 1e-14  -- {{Order of magnitude}} adds this, so we do too
    -- Find fractional digits, handling cases like inclean = '12.345e6'.
    local integer, dot, fraction = inclean:match('^(%d*)([' .. config.numdot .. ']?)(%d*)')
    if utype == 'temperature' then
        -- LATER: Give an error message if (invalue < in_unit_table.offset): below absolute zero?
        adjust = 0
        local kelvin = (invalue - in_unit_table.offset) * in_unit_table.scale
        if kelvin <= 0 then  -- can get zero, or small but negative value due to precision problems
            minprec = 2
        else
            minprec = 2 - math.floor(log10(kelvin) + fudge)  -- 3 sigfigs in kelvin
        end
    else
        if invalue == 0 or outvalue <= 0 then
            -- We are never called with a negative outvalue, but it might be zero.
            -- This is special-cased to avoid calculation exceptions.
            return 0
        end
        if out_unit_table.symbol == 'ft' and dot == '' then
            -- More precision when output ft with integer input value.
            adjust = -log10(in_unit_table.scale)
        else
            adjust = log10(math.abs(invalue / outvalue))
        end
        adjust = adjust + log10(2)
        -- Ensure that the output has at least two significant figures.
        minprec = 1 - math.floor(log10(outvalue) + fudge)
    end
    if dot == '' then
        prec = -integer:match('0*$'):len()  -- '12300' gives -2, but so does '12300e-5'
    else
        if #fraction == 0 and utype ~= 'temperature' then
            prec = 1  -- "123." has same precision as "123.0", like old template
        else
            prec = #fraction
        end
    end
    return math.max(math.floor(prec + adjust), minprec)
end

local function convert(value, in_unit, out_unit)
    local inscale = in_unit.scale
    local outscale = out_unit.scale
    if in_unit.invert ~= nil then
        if in_unit.invert * out_unit.invert < 0 then
            return 1 / (value * inscale * outscale)
        end
        return value * (inscale / outscale)
    elseif in_unit.offset ~= nil then
        return (value - in_unit.offset) * (inscale / outscale) + out_unit.offset
    else
        return value * (inscale / outscale)
    end
end

local function cvtround(invalue, inclean, parms)
    -- Return true, show, singular
    -- where
    --   show = '' if invalue is nil or ''. Otherwise:
    --   show = rounded, formatted string from converting invalue,
    --      using the rounding specified in parms.
    --   singular = true if result is positive, and (after rounding)
    --      is "1", or like "1.00".
    -- or return false, message if problem.
    -- This code combines convert/round because some rounding requires
    -- knowledge of what we are converting.
    -- TODO Limit values to avoid abuse (for example, can currently set
    -- precision to very large values like 999).
    local show, exponent, singular = '', nil, false
    if invalue == nil or invalue == '' then
        return true, show, singular
    end
    local outvalue = convert(invalue, parms.in_unit_table, parms.out_unit_table)
    local isnegative
    if outvalue < 0 then
        isnegative = true
        outvalue = -outvalue
    end
    local success
    local precision = parms.precision
    local sigfig = parms.sigfig
    local disp = parms.disp
    if precision then
        -- Ignore sigfig, disp.
        success, precision = require_integer(precision, 'Need value', 'precision "%s" must be an integer')
        if not success then return false, precision end
    elseif sigfig then
        -- Ignore disp.
        success, sigfig = require_integer(sigfig, 'Need value', 'sigfig "%s" must be an integer')
        if not success then return false, sigfig end
        if sigfig <= 0 then
            local msg = 'sigfig "%s" must be positive'
            return false, msg:format(parms.sigfig)
        end
        show, exponent = make_sigfig(outvalue, sigfig)
    elseif disp == '5' then
        outvalue = math.floor((outvalue / 5) + 0.5) * 5
        show = string.format('%.0f', outvalue)
    else
        precision = default_precision(inclean, invalue, outvalue, parms)
    end
    if precision then
        if precision >= 0 then
            if precision <= 8 then
                -- Add a fudge to handle common cases of bad rounding due to inability
                -- to precisely represent some values. This makes the following work:
                -- {{convert|-100.1|C|K}} and {{convert|5555000|um|m|2}}.
                -- Old template uses #expr round, which invokes PHP round().
                -- LATER: Investigate how PHP round() works.
                outvalue = outvalue + 2e-14
            end
            local fmt = '%.' .. string.format('%d', precision) .. 'f'
            show = string.format(fmt, outvalue)
        else
            precision = -precision  -- #digits to zero (in addition to digits after dot)
            local shift = 10 ^ precision
            if shift > outvalue then
                show = '0'  -- like old template, user can zero all digits
            else
                show = tostring(math.floor(outvalue/shift + 0.5))
                exponent = #show + precision
            end
        end
    end
    if (show == '1' or show:match('^1%.0*$') ~= nil) and not isnegative then
        -- Use match because on some systems 0.99999999999999999 is 1.0.
        singular = true
    end
    return true, format_number(show, exponent, isnegative), singular
end

local function linked(in_id, out_id, parms)
    -- Return in_id, out_id after modifying none, one, or both by replacing
    -- the text with a wikilink, if requested in template.
    local function substitute(link)
        -- TODO Work out how to insert a reasonable prefix in %s.
        return link:gsub('%%s', 'xxx', 1)
    end
    local lk = parms.lk
    if lk == 'in' or lk == 'on' then
        local link = parms.in_unit_table.link
        if link ~= nil then
            link = substitute(link)
            in_id = '[[' .. link .. '|' .. in_id .. ']]'
        end
    end
    if lk == 'out' or lk == 'on' then
        local link = parms.out_unit_table.link
        if link ~= nil then
            link = substitute(link)
            out_id = '[[' .. link .. '|' .. out_id .. ']]'
        end
    end
    return in_id, out_id
end

-- TODO: Think about when to use ' ' and when to use '&nbsp;'.
-- Old template always uses nbsp before a unit symbol, but seems inconsistent
-- before a unit name. Something suggested value 1000 was a threshold
-- (use nbsp for smaller values), but no conclusive results.
-- Possibly a concern is wrapping when using {{convert}} in a table
-- (don't want to force a column to be unnecessarily wide by using nbsp).
local disp_single = {
    ['or']    = '%s&nbsp;%s or %s&nbsp;%s',
    ['sqbr']  = '%s&nbsp;%s [%s&nbsp;%s]',
    ['comma'] = '%s&nbsp;%s, %s&nbsp;%s',
    ['slash'] = '%s&nbsp;%s / %s&nbsp;%s',
    ['s']     = '%s&nbsp;%s / %s&nbsp;%s',
    ['b']     = '%s&nbsp;%s (%s&nbsp;%s)',
}

local disp_double = {
    ['or']    = '%s%s%s&nbsp;%s or %s%s%s&nbsp;%s',
    ['sqbr']  = '%s%s%s&nbsp;%s [%s%s%s&nbsp;%s]',
    ['comma'] = '%s%s%s&nbsp;%s, %s%s%s&nbsp;%s',
    ['slash'] = '%s%s%s&nbsp;%s / %s%s%s&nbsp;%s',
    ['s']     = '%s%s%s&nbsp;%s / %s%s%s&nbsp;%s',
    ['b']     = '%s%s%s&nbsp;%s (%s%s%s&nbsp;%s)',
}

local function process(parms)
    -- Return true, s where s = final wikitext result (or false, message).
    -- TODO Clean up: avoid repeatedly accessing the same field in parms,
    -- and avoid the clumsy calculation of items that are not needed.
    local success, t
    success, t = units:lookup(parms.in_unit, parms.sp)
    if success then parms.in_unit_table = t else return success, t end
    if parms.out_unit == nil then           -- need to catch empty string also?
        success, t = defaultunits:lookup(parms.in_unit_table)
        if success then parms.out_unit = t else return success, t end
    end
    success, t = units:lookup(parms.out_unit, parms.sp, true)
    if success then parms.out_unit_table = t else return success, t end
    local in_utype = parms.in_unit_table.utype
    if in_utype ~= parms.out_unit_table.utype then
        local msg = 'Cannot convert %s to %s.[[Category:Convert dimension mismatch]]'
        return false, msg:format(in_utype, parms.out_unit_table.utype)
    end
    if parms.out_unit_table.combination then
        local msg = 'Combinations not implemented, %s'
        return false, msg:format(parms.out_unit)
    end
    local outshow1, outshow2, outsingular1, outsingular2
    success, outshow1, outsingular1 = cvtround(parms.in_value1, parms.in_clean1, parms)
    if not success then return success, outshow1 end
    success, outshow2, outsingular2 = cvtround(parms.in_value2, parms.in_clean2, parms)
    if not success then return success, outshow2 end
    local range = parms.range
    local disp = parms.disp
    local wikitext
    local inshow1, inshow2 = parms.in_show1, parms.in_show2
    local inkey, outkey = 'name2', 'name2'
    local insymkey, outsymkey = 'symbol', 'symbol'
    if parms.in_singular1 then     -- TODO how process second input value?
        inkey = 'name1'
    end
    if outsingular1 then
        outkey = 'name1'
    end
    if parms.sp == 'us' or parms.in_unit_table.sp_us then
        inkey = inkey .. '_us'
        insymkey = 'sym_us'
    end
    if parms.sp == 'us' or parms.out_unit_table.sp_us then
        outkey = outkey .. '_us'
        outsymkey = 'sym_us'
    end
    if parms.in_unit_table.usename then
        insymkey = inkey
    end
    if parms.out_unit_table.usename then
        outsymkey = outkey
    end
    local in_name = parms.in_unit_table[inkey]       -- will not need to calculate all of these
    local in_symbol = parms.in_unit_table[insymkey]
    local out_name = parms.out_unit_table[outkey]
    local out_symbol = parms.out_unit_table[outsymkey]
    local abbr = parms.abbr
    local in_id, out_id = in_symbol, out_symbol
    local istemperature = (in_utype == 'temperature')
    if abbr == 'on' then            -- all symbols
        -- Both symbols.
    elseif abbr == 'off' then       -- all names
        in_id = in_name
        out_id = out_name
    elseif abbr == 'in' then        -- input symbols
        -- Both symbols.
    elseif abbr == 'out' then       -- output symbols [is this just the default?]
        if not istemperature then
            in_id = in_name
        end
    elseif abbr == 'values' then    -- show only values
        -- TODO Probably more needed (no preceding space for a start).
        in_id = ''
        out_id = ''
    elseif abbr == 'mos' then       -- for ranges, abbreviate with input unit repeated
        -- LATER
    else                            -- default
        if not istemperature then
            in_id = in_name
        end
    end
    in_id, out_id = linked(in_id, out_id, parms)
    if range == nil then
        if disp == 'output only' then
            wikitext = '%s %s'
            wikitext = wikitext:format(outshow1, out_id)
        elseif disp == 'output number only' or disp == 'number' then
            wikitext = outshow1
        elseif disp == 'unit' then
            wikitext = in_id
        elseif disp == 'unit2' then
            wikitext = out_id
        elseif disp == 'flip' then
            wikitext = disp_single['b']
            wikitext = wikitext:format(outshow1, out_id, inshow1, in_id)
        else
            wikitext = disp_single[disp] or disp_single['b']
            wikitext = wikitext:format(inshow1, in_id, outshow1, out_id)
        end
    else
        -- TODO Need in_id, out_id (and more) here.
        wikitext = disp_double[disp] or disp_double['b']
        wikitext = wikitext:format(inshow1, range[1], inshow2, in_id, outshow1, range[2], outshow2, out_id)
    end
    if parms.sortable == 'on' then
        wikitext = ntsh(parms.in_value1, parms.debug) .. wikitext
    end
    return true, wikitext
end

local p = {}
-- A testing program can set the global variable 'is_test_run'.
-- The following sets global variable 'mw' to simulate what Scribunto will do.
local bodge = require(is_test_run and "mw" or "Module:mw")  -- fix up mw.text.tag

function p.convert(frame)
    config = get_config(frame)
    local pframe = frame:getParent()
    local success, parms, text
    success, parms = get_parms(pframe)
    if success then
        success, text = process(parms)
    else
        text = parms
    end
    if not success then
        local params = {style="color:black; background-color:orange;"}
        text = mw.text.tag({name="span", contents="[[Module talk:Convert|Conversion error]]: " .. text, params=params})
    end
    return text
end

return p