Help:Format produced by expr

From SacredWiki
Revision as of 22:01, 10 October 2013 by Schot (talk | contribs) (1 revision: Updating our Help Pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

The MediaWiki software simply passes on the literal result of the php computation, except that logical values are changed to 1 and 0. Therefore the format can depend on the server.

A number of type integer is displayed without rounding and in ordinary decimal notation:

  • {{#expr:trunc(2^52)}} → 4503599627370496
  • {{#expr:-trunc(2^52)}} → -4503599627370496

A number of type float is rounded to 14 digits. If a positive number is multiplied by -1 the format remains the same, except that "-" is prefixed. The following applies to positive numbers.

No trailing decimals are produced. In the case of scientific format the significand is greater than or equal to 1 and less than 10. The E is written as capital. In the case of a positive exponent "+" is prefixed. The exponent is 1 to 3 digits (it has no leading zeros). Numbers less than 1e-4 or greater than or equal to 1e18 are in scientific format.

Much of what follows was written in 2010, when there were inconsistencies in the use of scientific format, which according to http://bugs.php.net/43053 were due to a bug in the Zend Engine. This bug seems to have been fixed. The live examples below may show different results than before. The conclusions may have to be updated.

Numbers greater than or equal to 1e-4 which, after rounding to 14 digits, are not a multiple of 100,000 (which can only apply for numbers less than 1e18), are in ordinary decimal format.

For numbers which, after rounding to 14 digits, are multiples of 100,000 in the range 100,000 through 99,999,999,900,000, the criterion for using scientific format is not clear, except that if for the exact multiple of 100,000 scientific format is produced, then also for that number multiplied by a power of 10. Even for numbers which are equal after rounding to 14 digits both formats may occur. For exact multiples of 100,000 below 1,200,000 ordinary decimal notation is used.

Examples:

  • {{#expr:2^52}} → 4.5035996273705E+15
  • {{#expr:-(2^52)}} → -4.5035996273705E+15
  • {{#expr:1/7}} → 0.14285714285714
  • {{#expr:1e-6}} → 1.0E-6
  • {{#expr:1e-5}} → 1.0E-5
  • {{#expr:1e-4}} → 0.0001
  • {{#expr:1e-3}} → 0.001
  • {{#expr:1e-2}} → 0.01
  • {{#expr:1e-1}} → 0.1
  • {{#expr:1e-0}} → 1
  • {{#expr:1e1}} → 10
  • {{#expr:1e2}} → 100
  • {{#expr:1e3}} → 1000
  • {{#expr:1e4}} → 10000
  • {{#expr:1e5}} → 100000
  • {{#expr:1e6}} → 1000000
  • {{#expr:1e7}} → 10000000
  • {{#expr:1e8}} → 100000000
  • {{#expr:1e9}} → 1000000000
  • {{#expr:1e10}} → 10000000000
  • {{#expr:1e11}} → 100000000000
  • {{#expr:1e12}} → 1000000000000
  • {{#expr:1e13}} → 10000000000000
  • {{#expr:1e14}} → 1.0E+14
  • {{#expr:12e-6}} → 1.2E-5
  • {{#expr:12e-5}} → 0.00012
  • {{#expr:12e-4}} → 0.0012
  • {{#expr:12e-3}} → 0.012
  • {{#expr:12e-2}} → 0.12
  • {{#expr:12e-1}} → 1.2
  • {{#expr:12e-0}} → 12
  • {{#expr:12e1}} → 120
  • {{#expr:12e2}} → 1200
  • {{#expr:12e3}} → 12000
  • {{#expr:12e4}} → 120000
  • {{#expr:12e5}} → 1200000
  • {{#expr:12e6}} → 12000000
  • {{#expr:12e7}} → 120000000
  • {{#expr:12e8}} → 1200000000
  • {{#expr:12e9}} → 12000000000
  • {{#expr:12e10}} → 120000000000
  • {{#expr:12e11}} → 1200000000000
  • {{#expr:12e12}} → 12000000000000
  • {{#expr:12e13}} → 1.2E+14
  • {{#expr:123e-7}} → 1.23E-5
  • {{#expr:123e-6}} → 0.000123
  • {{#expr:123e-5}} → 0.00123
  • {{#expr:123e-4}} → 0.0123
  • {{#expr:123e-3}} → 0.123
  • {{#expr:123e-2}} → 1.23
  • {{#expr:123e-1}} → 12.3
  • {{#expr:123e-0}} → 123
  • {{#expr:123e1}} → 1230
  • {{#expr:123e2}} → 12300
  • {{#expr:123e3}} → 123000
  • {{#expr:123e4}} → 1230000
  • {{#expr:123e5}} → 12300000
  • {{#expr:123e6}} → 123000000
  • {{#expr:123e7}} → 1230000000
  • {{#expr:123e8}} → 12300000000
  • {{#expr:123e9}} → 123000000000
  • {{#expr:123e10}} → 1230000000000
  • {{#expr:123e11}} → 12300000000000
  • {{#expr:123e12}} → 1.23E+14
  • {{#expr:123e13}} → 1.23E+15

The function formatnum adds commas (on the left of the point only), but does not convert from or to scientific format:

  • {{formatnum:1234567.890123}} → 1,234,567.890123
  • {{formatnum:1234567.890123E16}} → 1,234,567.890123E16

The number output is suitable for many other calculation programs, also the scientific notation. In that sense output like 6E23 is more convenient than 6×1023.

Categorizing floats by the mathematical value of the string produced by expr

Most strings produced by expr (excluding error messages) each represent a unique mathematical value. However, as we have seen, for some multiples of 100,000 two strings occur, for example 4100000 and 4.1E+6. As we have seen in this case they even evaluate to different floats. However, for both strings #expr gives 4.1E+6, so for any two floats f1 and f2 for which {{#expr: f1 }} and {{#expr: f2 }} mathematically both represent 4100000, the following gives "true":

  • {{#ifexpr:{{#expr:{{#expr: f1 }}}}={{#expr:{{#expr: f2 }}}}|true|false}}

Also we can use:

  • {{#ifeq:{{#expr: f1 }}|{{#expr: f2 }}|true|false}}

Examples:

  • {{#ifexpr:4100000=4.1E+6|true|false}} → false
  • {{#ifexpr:{{#expr:4100000}}={{#expr:4.1E+6}}|true|false}} → true
  • {{#ifeq:4100000|4.1E+6|true|false}} → true
  • {{#ifexpr:{{#expr:4100000}}={{#expr:4100000.0000001}}|true|false}} → false
  • {{#ifeq:4100000|4100000.0000001|true|false}} → false

The experiments suggest that this applies in general.

Another method is:

  • {{#ifexpr:abs({{#expr: f1 }}-{{#expr: f2 }})e15<=abs( f1 )|true|false}}

or, if f2 is known to be nonzero:

  • {{#ifexpr:abs({{#expr: f1 }}/{{#expr: f2 }}-1)e15<1|true|false}}</code>

Examples:

  • {{#ifexpr:abs(4100000-4.1E+6)e15<=abs4100000|true|false}} → true
  • {{#ifexpr:abs(4100000-4100000.0000001)e15<=abs4100000|true|false}} → false
  • {{#ifexpr:abs(4100000/4.1E+6-1)e15<=1|true|false}} → true
  • {{#ifexpr:abs(4100000/4100000.0000001-1)e15<=1|true|false}} → false

Application

One method of producing a consistent number format involves deciding whether the mathematical value of a string produced by expr is a multiple of 100,000: if so, the number divided by 100,000, followed by 00000 is produced; if not, #expr is used.

With exact computations this could be done with the condition ({{{1}}}/1e5round0)e5={{{1}}}, where {{{1}}} is the given string. Rounding errors are small enough that by itself the comparison does not give false positives, but without special provisions we get false negatives. Thus we have to prevent false negatives without introducing false positives.

The left-hand side evaluates to an exact multiple of 100,000, provided that the number is in a range where that multiple is an exact float. However, the right-hand side may for example be 4.1E+6, which, as we have seen, does not evaluate to 4,100,000. If {{{1}}} is not 0 or -0, we can use for example the condition

  • abs(({{{1}}}/1e5round0)e5/{{{1}}}-1)e15<1

This bound of 1e-15 on the relative difference is larger than the combined effect of some relative rounding errors of up to 1.1e-16 each, while smaller than the relative spacing of 14-digit numbers, which is at least 1e-14.

See also Template:Expr with Template:Expr/1, and w:Template:Rndfrac.

Very small numbers

For numbers which in absolute value are less than 1e-310 every float has a different output string (each representing a different mathematical value). and multiple 14-digit numbers are rounded to the same float:

  • {{#expr:9.999999999999e-311}} → 9.9999999999985E-311
  • {{#expr:9.9999999999991e-311}} → 9.9999999999985E-311
  • {{#expr:9.9999999999992e-311}} → 9.9999999999985E-311
  • {{#expr:9.9999999999993e-311}} → 9.999999999999E-311
  • {{#expr:9.9999999999994e-311}} → 9.999999999999E-311
  • {{#expr:9.9999999999995e-311}} → 9.999999999999E-311
  • {{#expr:9.9999999999996e-311}} → 9.999999999999E-311
  • {{#expr:9.9999999999997e-311}} → 9.999999999999E-311
  • {{#expr:9.9999999999998e-311}} → 9.9999999999995E-311
  • {{#expr:9.9999999999999e-311}} → 9.9999999999995E-311
  • {{#expr:1e-310}} → 1.0E-310
  • {{#expr:1.0000000000001e-310}} → 1.0000000000001E-310
  • {{#expr:1.0000000000002e-310}} → 1.0000000000002E-310
  • {{#expr:1.0000000000003e-310}} → 1.0000000000003E-310
  • {{#expr:1.0000000000004e-310}} → 1.0000000000004E-310

See also

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · mw: · b: · s: · mw:Manual · Google
Versions of this help page (for other languages see further)
Meta · Wikinews · Wikipediahttp://en.wikipedia.org/index.php/Help:Format_produced_by_expr · Wikiquote · Wiktionary
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 · URLs · 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 · Talk page · Signatures · Sandbox · Legal issues for editors