Difference between revisions of "Template:Calculate increment"
(more compact, more exact and more general) |
imported>CBDunkerson (Using abs parserfunction instead of abs template) |
||
| Line 1: | Line 1: | ||
<includeonly>{{#expr: | <includeonly>{{#expr: | ||
| − | {{#ifexpr:( {{abs | + | {{#ifexpr:( {{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000 |
| − | round 2-{{len|{{abs | + | round 2-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}} |
| − | ){{#ifexpr:{{len|{{abs | + | ){{#ifexpr:{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}>=1 |
| − | |{{loop|{{#expr: {{len|{{abs | + | |{{loop|{{#expr: {{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}-2}}|/10}} |
| − | |{{loop|{{#expr:-{{len|{{abs | + | |{{loop|{{#expr:-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}+2}}|*10}} |
}}<14|1| | }}<14|1| | ||
| − | {{#ifexpr:( {{abs | + | {{#ifexpr:( {{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000 |
| − | round 2-{{len|{{abs | + | round 2-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}} |
| − | ){{#ifexpr:{{len|{{abs | + | ){{#ifexpr:{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}>=1 |
| − | |{{loop|{{#expr: {{len|{{abs | + | |{{loop|{{#expr: {{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}-2}}|/10}} |
| − | |{{loop|{{#expr:-{{len|{{abs | + | |{{loop|{{#expr:-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}+2}}|*10}} |
}}<35|2| | }}<35|2| | ||
| − | {{#ifexpr:( {{abs | + | {{#ifexpr:( {{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000 |
| − | round 2-{{len|{{abs | + | round 2-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}} |
| − | ){{#ifexpr:{{len|{{abs | + | ){{#ifexpr:{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}>=1 |
| − | |{{loop|{{#expr: {{len|{{abs | + | |{{loop|{{#expr: {{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}-2}}|/10}} |
| − | |{{loop|{{#expr:-{{len|{{abs | + | |{{loop|{{#expr:-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}+2}}|*10}} |
}}<70|5| | }}<70|5| | ||
10}}}}}} | 10}}}}}} | ||
| − | {{#ifexpr: {{len|{{abs | + | {{#ifexpr: {{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}} }}>=1 |
| − | |{{loop|{{#expr: {{len|{{abs | + | |{{loop|{{#expr: {{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}} }}-2}}|*10}} |
| − | |{{loop|{{#expr:-{{len|{{abs | + | |{{loop|{{#expr:-{{len|{{#expr:abs ({{{2|2}}})-({{{1|1}}})}}*1000000}}+8}}|/10}} |
}} | }} | ||
}}</includeonly><noinclude>{{documentation}}</noinclude> | }}</includeonly><noinclude>{{documentation}}</noinclude> | ||
Revision as of 02:03, 23 July 2008
Usage
This template takes two numbers and calculates a suitable increment for {{scalemarkers}} based on the difference between these numbers. Increments are 1, 2 or 5 times an appropriate power of 10.
These numbers, the from value and the to value, are input as unnamed parameters.
{{calculate increment|p1|p2}}
The absolute value, d, of the difference between these is calculated.
- d = | p1 − p2 |
This can be expressed in scientific notation as follows.
- d = a × 10n
Where 1 ≤ a < 10 and n is an integer.
The output of the template is as follows.
For 1 ≤ a < 1.35 the output is the number 1 × 10n − 1 For 1.35 ≤ a < 3.45 the output is the number 2 × 10n − 1 For 3.45 ≤ a < 6.95 the output is the number 5 × 10n − 1 For 6.95 ≤ a < 10 the output is the number 1 × 10n
- Note
- The values are returned as ordinary numbers (for use in other templates) not in scientific notation (e.g. 5000 not 5 × 103).
- The template code actually uses 1.4, 3.5 and 7 as cut offs but these are reduced by 0.05 due to rounding.
- Examples
- Technical details
The main template page calculates d, the absolute value of the difference between the two inputs. Using {{order of magnitude}} the order of magnitude, n, of d is calculated. d and 10n − 1 are sent as parameters 1 and 2 respectively to a subpage, {{calculate increment/calc}}. This subtemplate uses a #switch to determine the correct factor, 1, 2, 5 or 10, then multiplies by 10n − 1.
| The above documentation is transcluded from Template:Calculate increment/doc. (edit | history) Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages. Please add categories to the /doc subpage. Subpages of this template. |