Help:Round

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search

Operator round of #expr and #ifexpr (PHP function round) rounds the number on the left to the nearest multiple of 1/10 raised to the power given on the right; if two are equally near, rounding is away from zero.

  • {{numfh|123456789012345669round-1}} → 123,456,789,012,345,664 (ca. 1.2e17) 1.b69b4ba630f34hex*2^56
  • {{rnd|123456789012345669|-1}} → 1.2345678901235×1017
  • {{#expr: 30/7 round 0}} gives 4 [1]
  • {{#expr: 3456 round -2}} gives 3500 [2]

The function round can make sense in any range, if the rounding of a large number is crude enough:

  • {{#expr:(4/7)*1e-290round300}} → 5.714285714E-291
  • {{#expr:(4/7)*1e290round-280}} → 5.714285714E+289

An attempt of very fine rounding of a very large number (which would not make sense anyway) can give INF, since it involves a division of the very large number by a very small one:

  • {{#expr:1e-92round400}} → 1.0E-92
  • {{#expr:1e-91round400}} → 1.0E-91
  • {{#expr:1e108round200}} → 1.0E+108
  • {{#expr:1e109round200}} → 1.0E+109
  • {{#expr:1e208round100}} → 1.0E+208
  • {{#expr:1e209round100}} → 1.0E+209

Rounding with a strongly negative rounding parameter correctly gives 0, except that if it is -324 or less the original number is returned.

With rounding parameter 0, 0dd integers with absolute value between 2^52 and 2^53 are not rounded correctly to itself, they are rounded away from zero to an even number:

  • {{numf|2^52+1}} → 4,503,599,627,370,497
  • {{numf|(2^52+1)round0}} → 4,503,599,627,370,497

This is an unfortunate consequence of the general method of adding .5, rounding the result with the round half to even method (see also Help:Calculation accuracy#Rounding), and truncating to an integer. Template:Round0 avoids this problem.

Below and above that range, round works correctly:

  • {{numf|4503599627370494round0}} → 4,503,599,627,370,494
  • {{numf|4503599627370494.5round0}} → 4,503,599,627,370,494.5
  • {{numf|4503599627370495round0}} → 4,503,599,627,370,495
  • {{numf|4503599627370495.5round0}} → 4,503,599,627,370,495.5
  • {{numf|2^53+2}} → 9,007,199,254,740,994
  • {{numf|(2^53+2)round0}} → 9,007,199,254,740,994

This phenomena, that an internal number is already a multiple of the required power of 10, but the round function changes it anyway, occurs only with second argument 0, because it can only happen if the spacing of the potential results of the rounding is equal to the spacing of the representable numbers. Inaccuracies that do occur:

  • {{numf|(1e16+14)round-1}} → 10,000,000,000,000,014

To check whether a float below 2^63 is an integer, use floor, ceil or trunc instead.

Errors with positive second argument[edit]

Apart from the issue of "deciding to which value to round", there is the issue of computing this value. One might expect that this round number, if representable as float, as in the case of an integer <= 2^53, is returned exactly.

However, if the round function has a negative second argument, an integer is divided by a negative power of 10, which may give a rounding error:

  • {{numfh|1e5round-5}} → 100,000 (ca. 1e5) 1.86a0000000000hex*2^16

because:

  • {{numfh|1/1e-5}} → 99,999.99999999999 (ca. 10e4) 1.869ffffffffffhex*2^16

Workarounds:

  • {{numfh|1e5round-5round0}} → 100,000 (ca. 1e5) 1.86a0000000000hex*2^16 (apply round0 to the result; in general: {{{1}}}round{{{2}}}{{#ifexpr:{{{2}}}<0|round0}})
  • {{numfh|(1e5/1e5round0)*1e5}} → 100,000 (ca. 1e5) 1.86a0000000000hex*2^16 (convert to round0; in general: ({{{1}}}{{#ifexpr:{{{2}}}<0|/1e-{{{2}}}round0)|e{{{2}}}round0)/1}}e abs{{{2}}})

Examples:

  • {{Help:Round/1|123456.78|-5}} → 100,000 100,000
  • {{Help:Round/1|123456.78|1}} → 123,456.8 123,456.8

Examples[edit]

  • {{numfh|123.7round-324}} → 0
  • {{numfh|123.7round-323}} → 0
  • {{numfh|1234.7round-324}} → 0
  • {{numfh|1234.7round-323}} → 0
  • {{numfh|12.7round-324}} → 0
  • {{numfh|12.7round-323}} → 0
  • {{numfh|123.7round-4}} → 0
  • {{numfh|123.7round-3}} → 0
  • {{numfh|123.7round-2}} → 100 (ca. 1e2) 1.9000000000000hex*2^6
  • {{numfh|123.7round-1}} → 120 (ca. 1.2e2) 1.e000000000000hex*2^6
  • {{numfh|123.7round0}} → 124 (ca. 1.2e2) 1.f000000000000hex*2^6
  • {{numfh|123.7round1}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6
  • {{numfh|123.7round2}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6
  • {{numfh|123.7round3}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6
  • {{numfh|123.7round4}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6
  • {{numfh|123.7round306}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6
  • {{numfh|123.7round307}} → 123.7 (ca. 1.2e2) 1.eeccccccccccdhex*2^6

See also[edit]

Links to other help pages[edit]

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Stop words · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Testing · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Embed page
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Languages: