• Home
  • Random
  • Nearby
  • Log in
  • Settings
Donate Now If Wikipedia is useful to you, please give today.
  • About Wikipedia
  • Disclaimers
Wikipedia

Template:Precision

  • Template
  • Talk
  • Language
  • Watch
  • View source
Template documentation[view] [edit] [history] [purge]
WarningThis template is used on approximately 290,000 pages.
To avoid major disruption and server load, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This template uses Lua:
  • Module:Math (sandbox)


The Template:Precision determines the precision (as a count of decimal digits) for any amount, large or negative, using a fast algorithm. It can also handle a trailing decimal point (such as "15." or "-41.") or trailing zeroes (such as "15.34000" having precision as 5 decimal digits). For fractional input it returns the base ten logarithm of the numerator.

Examples

{{Precision|1111.123456789}} 9
{{Precision|1111.12345678}} 8
{{Precision|1111.1234567}} 7
{{Precision|1111.123456}} 6
{{Precision|1111.12345}} 5
{{Precision|1111111111.12345678}} 8
{{Precision|1111111111.1234567}} 7
{{Precision|1111111111.123456}} 6
{{Precision|1111111111.12345}} 5
{{Precision|1111111111.1234}} 4
{{Precision|1111111111.123}} 3
{{Precision|1111111111.12}} 2
{{Precision|1111111111.1}} 1
{{Precision|1111111111.10}} 2
{{Precision|1111111111.100}} 3
{{Precision|1111111111.1000}} 4
{{Precision|1111111111.10000}} 5
{{Precision|1111111111}} 0
{{Precision|1111111110}} -1
{{Precision|1111111100}} -2
{{Precision|1111111000}} -3
{{Precision|1111110000}} -4
{{Precision|1111100000}} -5
{{Precision|1111000000}} -6
{{Precision|1110000000}} -7
{{Precision|1100000000}} -8
{{Precision|0}} 0
{{Precision|1}} 0
{{Precision|22.45}} 2
{{Precision|22.12345}} 5
{{Precision|22}} 0
{{Precision|22000}} -3
{{Precision|-15.275}} 3
{{Precision|-15.2500}} 4
{{Precision|23000222000111.432}} 3
{{Precision|-15.123}} 3
{{Precision|0.09}} 2
{{Precision|0.88}} 2
{{Precision|880000}} -4
{{Precision|90000000}} -7

Known bugs

  • For numbers in scientific notation, the precision is typically returned as too low by 1 decimal place. Example: {{precision |7.1234E+06}} → -2 (should be precision as 4 decimal digits, not 3).

Technical notes

  • NOTE A1: This template determines the precision of decimals by counting the length of the numeric string (in a #switch comparing lengths of padded strings), then subtracting integer length, minus the decimal point, and minus 1 if negative. For integers, 1 place is subtracted for each trailing 0 on the integer. For fractions, any prior count is cleared x 0, then returns the base ten logarithm of denominator: (..prior...)*0 + (ln denom / ln 10).
  • NOTE D2: The check, for whole integers, compares the amount versus appending "0" at the end: when the amount is a decimal, then the value is unchanged by appending 0 at the end: so 5.23 = 5.230 is true, whereas for whole integers, it would be: 5 = 50 as false, due to values becoming n*10 for integer n. So, for integer n, the check rejects: n = n0 as false; hence n is integer.
  • NOTE M3: The magnitude of the integer portion is calculated by logarithm of the floor of absolute value (divided by natural logarithm of 10 to adjust for e=2.71828*), as: ln (floor( abs(-0.050067) )+0.99 )/ln10 Function floor(x) trims the decimal part, to leave the whole count: 0-9 yield 0, 10-19 as 1, 1000-1999 as 3. The abs(x) avoids floor of negatives, floor(-0.1)= -1, hence using abs(x) ensures -0.1 floors to 0 not -1. Near zero, the +0.99 avoids invalid log of 0, but does not round-up any decimals, already floored as nnn.00. Complexity is 6 operations: floor of abs( {1} ) +0.99 then log10x (lnx ÷ ln10), then floor that logarithm ratio. Decimals -1 < x < 1 yield -1, avoiding log 0.001 = -3.
  • NOTE N4: Nesting of if-else and nested templates is kept to a minimum, due to the MediaWiki 1.6 limit of 40 levels of if-logic for all nested templates used together. Template {ordomag} was omitted to avoid 2 more levels of nested templates. Template {Precision} had 8 levels, and this template was trimmed to only 5 levels.
  • NOTE S5: The #switch is run with "x" prepended in front of the amount, otherwise a #switch will compare as numeric where "2" would match "2.0" even though "2" is length 1 so "x2" no longer matches with "x2.0" as non-numeric. The #switch will exit on the first match, so smaller lengths are compared first, to avoid extra comparisons for more rare, longer numeric strings up to 41 long.
  • NOTE W6: The check for integers with whole end-zeroes uses typical n=n/10*10, for each power of 10, where whole millions match: {{#ifexpr: {1}=floor( {1}/1E6 )*1E6| }} Previously, {Precision} had tried to use "round" to detect end-zeroes but "round" loses precision at -5, so, n00000 round -5 differs from n00000 slightly, and comparisons to exact rounded amounts failed to match some numbers when 6 or more zeroes "n000000".
  • NOTE Z7: The check on zero for any .00000 compares adding 1 to the amount, versus appending "1" at the end: if the amount is a decimal, then adding 1 will be larger than appending 1 at the end: 0.00 + 1 > 0.001, whereas for whole zero, it would be: 0+1 > 01 as false, due to the value being the same. So, for integer 0, the check rejects: 0+1 > 01 as false; hence whole 0 is integer.

See also

  • Significant figures
  • v
  • t
  • e
Math templates
  • Functions
  • Numeral systems
Functions
  • elementary arithmetic
  • precision
  • val (value formatting)
  • percentage
Numeral systems
  • {{#invoke:BaseConvert|XtoY}}
  • binary
  • decimal2Base
  • hex2dec
  • hexadecimal
  • octal
  • quaternary
  • rn
  • roman
  • ternary
  • vigesimal
Conversions
convert many units (see: list)
cvt abbreviated {{convert}}
convinfobox {{convert}} for infoboxes
bbl to t barrels of oil to tonnes
long ton long hundredweights, quarters and pounds to kilograms;
long tons and hundredweights to pounds and metric tons
miles-chains miles and chains to kilometres linking "chains"
decdeg degrees, minutes, and seconds to decimal degrees
deg2dms decimal degrees to degrees, minutes, and seconds
deg2hms decimal degrees to hour angle (in hours, minutes, and seconds)
hms2deg hour angle (in hours, minutes, and seconds) to decimal degrees
inflation calculate inflation of Consumer Price Index-related prices
pop density population density in an area
track gauge railway track gauges
Notation and formatting
bigmath bigger font to match TeX \displaystyle (standalone formulas only) math short text-based formulas
mathcal [mathematical] calligraphic font; alternative to LaTeX \mathcal{...} tombstone symbol indicating the end of a proof
mvar individual italicized maths variables in normal text val measurement values, uncertainties and units
  • overline
  • underline
a line set above/below a sequence of characters vec various overarrows, underarrows, etc.
abs absolute values (paired vertical lines)
  • langle
  • rangle
  • angbr
  • angular brackets
  • bra-ket
  • braket
  • bra
  • ket
  • bra–ket notation
  • ldelim
  • rdelim
  • multiline delimiters (2–5 lines inclusive)
    ceil, floor calculations :mw:Help:#expr; formatting indicators ⌈3.14⌉, ⌊3.14⌋ (no calculation performed) pars parentheses that can be resized (∑)
    fraction slant fractions 3⁄5 (not for maths/science articles; use standing or upright fractions {{sfrac}} instead) sfrac "standing" or upright fractions ⁠3/5⁠ (use in maths/science articles instead of{{fraction}})
    intmath integral symbols
  • sub
  • sup
  • su
  • subscripts and superscripts
  • overset
  • underset
  • arbitrary characters/diacritics set above/below one another    
    tmath Wrap TeX in <math> tags
  • Boxes
  • Tags
  • Notices
  • BoxesTags
    • arithmetic operations
    • calculus
    • Infobox mathematical function
    • functions
    • metricate
    • undue precision
    • units attention
    • Category
    • Module:Math
    The above documentation is transcluded from Template:Precision/doc. (edit | history)
    Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages.
    Add categories to the /doc subpage. Subpages of this template.
    Retrieved from "https://en.wikipedia.org/w/index.php?title=Template:Precision&oldid=772120865"
    Last edited on 25 March 2017, at 12:38

    Languages

      • Afrikaans
      • العربية
      • Արեւմտահայերէն
      • অসমীয়া
      • Basa Bali
      • বাংলা
      • Banjar
      • 閩南語 / Bân-lâm-gí
      • Башҡортса
      • Беларуская (тарашкевіца)
      • भोजपुरी
      • Bosanski
      • Brezhoneg
      • Català
      • Chavacano de Zamboanga
      • ChiShona
      • Dansk
      • Ελληνικά
      • فارسی
      • Fiji Hindi
      • Føroyskt
      • Galego
      • ગુજરાતી
      • 한국어
      • Հայերեն
      • Ilokano
      • IsiZulu
      • Íslenska
      • Jawa
      • ქართული
      • Kongo
      • Kurdî
      • Кыргызча
      • Latina
      • Latviešu
      • Luganda
      • Magyar
      • Madhurâ
      • मैथिली
      • Македонски
      • മലയാളം
      • मराठी
      • مصرى
      • Bahasa Melayu
      • Монгол
      • မြန်မာဘာသာ
      • नेपाली
      • 日本語
      • Novial
      • ଓଡ଼ିଆ
      • ਪੰਜਾਬੀ
      • Pangasinan
      • پنجابی
      • ပအိုဝ်ႏဘာႏသာႏ
      • پښتو
      • ភាសាខ្មែរ
      • Português
      • Română
      • Русский
      • Gagana Samoa
      • संस्कृतम्
      • Scots
      • සිංහල
      • Simple English
      • سنڌي
      • Slovenčina
      • Slovenščina
      • کوردی
      • Српски / srpski
      • Sunda
      • Svenska
      • Tagalog
      • தமிழ்
      • Татарча / tatarça
      • တႆး
      • తెలుగు
      • ไทย
      • Türkçe
      • Українська
      • اردو
      • Tiếng Việt
      • Winaray
      • Yorùbá
      • 粵語
      • 中文
      • ᥖᥭᥰ ᥖᥬᥲ ᥑᥨᥒᥰ
      • ⵜⴰⵎⴰⵣⵉⵖⵜ ⵜⴰⵏⴰⵡⴰⵢⵜ
      Wikipedia
      • Wikimedia Foundation
      • Powered by MediaWiki
      • This page was last edited on 25 March 2017, at 12:38 (UTC).
      • Content is available under CC BY-SA 4.0 unless otherwise noted.
      • Privacy policy
      • About Wikipedia
      • Disclaimers
      • Contact Wikipedia
      • Code of Conduct
      • Developers
      • Statistics
      • Cookie statement
      • Terms of Use
      • Desktop