Help:Format produced by expr
| 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{{#expr:trunc1100000}}→ 1100000{{#expr:trunc1200000}}→ 1200000{{#expr:trunc1300000}}→ 1300000{{#expr:trunc4100000}}→ 4100000
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.
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. According to http://bugs.php.net/43053, the inconsistencies are due to a bug in the Zend Engine, which can be fixed with http://int-e.home.tlink.de/php/php-printf.patch. This patch involves a change in the files zend_strtod.c and snprintf.c.
Examples:
{{#expr:99999.9999999995}}→ 100000 (smallest by expr produced number greater than 1 in scientific notation){{#expr:1e5}}→ 100000{{#expr:100000.000000001}}→ 100000{{#expr:2^52}}→ 4.5035996273705E+15{{#expr:-(2^52)}}→ -4.5035996273705E+15{{#expr:1100000}}→ 1100000{{#expr:1200000}}→ 1200000{{#expr:1300000}}→ 1300000{{#expr:4100000}}→ 4100000{{#expr:1/7}}→ 0.14285714285714{{#expr:999999999999994880}}→ 9.9999999999999E+17 (largest float giving ordinary decimal notation){{#expr:999999999999994944}}→ 9.9999999999999E+17 (largest number giving ordinary decimal notation)
{{#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:11e-6}}→ 1.1E-5{{#expr:11e-5}}→ 0.00011{{#expr:11e-4}}→ 0.0011{{#expr:11e-3}}→ 0.011{{#expr:11e-2}}→ 0.11{{#expr:11e-1}}→ 1.1{{#expr:11e-0}}→ 11{{#expr:11e1}}→ 110{{#expr:11e2}}→ 1100{{#expr:11e3}}→ 11000{{#expr:11e4}}→ 110000{{#expr:11e5}}→ 1100000{{#expr:11e6}}→ 11000000{{#expr:11e7}}→ 110000000{{#expr:11e8}}→ 1100000000{{#expr:11e9}}→ 11000000000{{#expr:11e10}}→ 110000000000{{#expr:11e11}}→ 1100000000000{{#expr:11e12}}→ 11000000000000{{#expr:11e13}}→ 1.1E+14
{{#expr:13e-6}}→ 1.3E-5{{#expr:13e-5}}→ 0.00013{{#expr:13e-4}}→ 0.0013{{#expr:13e-3}}→ 0.013{{#expr:13e-2}}→ 0.13{{#expr:13e-1}}→ 1.3{{#expr:13e-0}}→ 13{{#expr:13e1}}→ 130{{#expr:13e2}}→ 1300{{#expr:13e3}}→ 13000{{#expr:13e4}}→ 130000{{#expr:13e5}}→ 1300000{{#expr:13e6}}→ 13000000{{#expr:13e7}}→ 130000000{{#expr:13e8}}→ 1300000000{{#expr:13e9}}→ 13000000000{{#expr:13e10}}→ 130000000000{{#expr:13e11}}→ 1300000000000{{#expr:13e12}}→ 13000000000000{{#expr:13e13}}→ 1.3E+14
{{#expr:41e-6}}→ 4.1E-5{{#expr:41e-5}}→ 0.00041{{#expr:41e-4}}→ 0.0041{{#expr:41e-3}}→ 0.041{{#expr:41e-2}}→ 0.41{{#expr:41e-1}}→ 4.1{{#expr:41e-0}}→ 41{{#expr:41e1}}→ 410{{#expr:41e2}}→ 4100{{#expr:41e3}}→ 41000{{#expr:41e4}}→ 410000{{#expr:41e5}}→ 4100000{{#expr:41e6}}→ 41000000{{#expr:41e7}}→ 410000000{{#expr:41e8}}→ 4100000000{{#expr:41e9}}→ 41000000000{{#expr:41e10}}→ 410000000000{{#expr:41e11}}→ 4100000000000{{#expr:41e12}}→ 41000000000000{{#expr:41e13}}→ 4.1E+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
{{#expr:456e-7}}→ 4.56E-5{{#expr:456e-6}}→ 0.000456{{#expr:456e-5}}→ 0.00456{{#expr:456e-4}}→ 0.0456{{#expr:456e-3}}→ 0.456{{#expr:456e-2}}→ 4.56{{#expr:456e-1}}→ 45.6{{#expr:456e-0}}→ 456{{#expr:456e1}}→ 4560{{#expr:456e2}}→ 45600{{#expr:456e3}}→ 456000{{#expr:456e4}}→ 4560000{{#expr:456e5}}→ 45600000{{#expr:456e6}}→ 456000000{{#expr:456e7}}→ 4560000000{{#expr:456e8}}→ 45600000000{{#expr:456e9}}→ 456000000000{{#expr:456e10}}→ 4560000000000{{#expr:456e11}}→ 45600000000000{{#expr:456e12}}→ 4.56E+14{{#expr:456e13}}→ 4.56E+15
{{#expr:-11e5}}→ -1100000{{#expr:-12e5}}→ -1200000{{#expr:-13e5}}→ -1300000{{#expr:-41e5}}→ -4100000{{#expr:-123e5}}→ -12300000{{#expr:-456e5}}→ -45600000
For some representable round numbers, notably some multiples of 100,000, scientific notation is produced, which, if reused in an expression, is not even exactly equal to the original number:
{{numfh|4.1e6}}→ 4,099,999.9999999995 (ca. 4.1e6)1.f47cfffffffffhex*2^21
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.
Contents |
[edit] More on multiples of 100,000
Conclusions suggested by tests:
A float sufficiently below a multiple of 100,000, which mathematically rounded to 14 digits gives that multiple, always gives scientific notation. A float sufficiently above a multiple of 100,000, which mathematically rounded to 14 digits gives that multiple, always gives ordinary decimal notation.
In cases where a multiple of 100,000 gives scientific notation which itself evaluates to a float that is not equal to that multiple, it is always a float below that multiple, and correspondingly, the significand evaluates to a float below its decimal value. The scientific notation produced by the multiple of 100,000 always gives itself. Therefore {{#expr:{{#expr: f }}}} gives the same string for all floats f for which {{#expr: f }} mathematically represents the same number.
{{#expr:1e5-4e-10}}→ 100000{{#expr:1e5}}→ 100000{{#expr:1e5+4e-9}}→ 100000
{{#expr:10e5-4e-9}}→ 1000000{{#expr:10e5}}→ 1000000{{#expr:10e5+4e-8}}→ 1000000
{{#expr:11e5-4e-8}}→ 1100000{{#expr:11e5}}→ 1100000{{#expr:11e5+4e-8}}→ 1100000
{{#expr:12e5-4e-8}}→ 1200000{{#expr:12e5}}→ 1200000{{numfh|1.2}}→ 1.2 (ca. 1.2e0)1.3333333333333hex*2^0{{#expr:1.2e6}}→ 1200000{{#expr:12e5+4e-8}}→ 1200000
{{#expr:41e5-4e-8}}→ 4100000{{#expr:41e5}}→ 4100000{{numfh|4.1}}→ 4.1 (ca. 4.1e0)1.0666666666666hex*2^2{{numfh|4.1e6}}→ 4,099,999.9999999995 (ca. 4.1e6)1.f47cfffffffffhex*2^21{{#expr:4.1e6}}→ 4100000{{#expr:41e5+4e-8}}→ 4100000
[edit] 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
[edit] 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:Rnd/b1.
[edit] 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
-
[edit] See also
[edit] 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 below)
- Meta | Wikinews | Wikipedia | 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 | Sandbox | Legal issues for editors
| Language: | English |
|---|