Misplaced Pages

User:Σ/Testing facility/Rangemap

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Σ | Testing facility Documentation[view] [edit] [history] [purge]

Usage

{{rangemap
| value =  the number to map
| from_low = the lower bound of the value's current range
| from_high = the upper bound of the value's current range
| to_low = the lower bound of the value's target range
| to_high = the upper bound of the value's target range
| precision = the number of decimals to round to
}}

or, without specifying argument names:

{{rangemap|value|from_low|from_high|to_low|to_high}}

Description

Re-maps a number from one range to another. That is, a value of from_low would get mapped to to_low, a value of from_high to to_high, values in-between to values in-between, etc.

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful.

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so this template may be used to reverse a range of numbers, for example

{{rangemap|{{{x}}}|1|50|50|1}}

The template also handles negative numbers well, so that this example

{{rangemap|{{{x}}}|1|50|50|-100)

is also valid and works well.

Appendix

For the mathematically inclined, here's the expression:

(value - from_low) * (from_high - from_low) / (to_high - to_low) + to_low
The above documentation is transcluded from User:Σ/Testing facility/Rangemap/doc. (edit | history)
Editors can experiment in this template's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this page.