User:Wikid77/numsense
1
The User:Wikid77/numsense calculates the precision of a number (parameter 1) based on the number sense of how precise the amount has been specified. Contrary to typical engineering precision, where whole multiples of 10 are considered rounded to the nearest 10, this template treats the numbers 10, 20, 30 (etc.) as being rounded to the nearest 1 unit, as precision 0. Multiples of 100 are treated as rounded to nearest 10, with precision -1. Examples:
- {{numsense |23}} → 0
- {{numsense |40}} → 0
- {{numsense | 9}} → 0
- {{numsense |10}} → 0
- {{numsense |10 }} → -1
- {{numsense |2300}} → -1
- {{numsense |67.00}} → 2
- {{numsense |67.000}} → 3
- {{numsense |59.004500}} → 6
- {{numsense |67000}} → -3
- {{numsense |-67}} → 0
- {{numsense |0.00}} → 0
- {{numsense |34.56E7}} → -4
In decimal precision, the number 10 is treated as between 9.5 < 10 < 10.5, rather than 5<10<15. Consequently, {numsense} gives 10, 40, 70 (etc.) the precision 0, same as the numbers 1-9. Also, numerals with trailing zeroes count the extra zeroes as higher precision, so 8.200 has precision 3 for the 3 decimal places.
Performance considerations
[edit]In runtime speed, User:Wikid77/numsense processes numbers at the rate of about 330 numbers per second. For most cases (0-99,000), the expansion depth is only 4 levels of the MediaWiki wp:expansion depth limit, but some numbers over 8 digits might use 6 levels. For simplicity in reviewing the basic algorithm, this template uses markup to handle small numbers, to avoid the complexity of a separate Lua script module using the Scribunto interface to pass data as frame arguments. However, for large amounts, the Lua-based {precision} is invoked from Module:Math.
See also
[edit]- Template:Precision - gets numeric engineering precision (-1 for 10)
Category:Mathematical function templates
Category:Templates with minimal expansion depth