Jump to content

Template:Trunc

From Meta, a Wikimedia project coordination wiki
Template documentation


This template produces an expression for the truncated value of a given expression. If the value is in the range of integer-type numbers (in the range −9,223,372,036,854,776,000 through 9,223,372,036,854,776,000), the resulting expression is of type integer.

The template also serves as a tool to enter an integer larger than 9,007,199,254,740,992, avoiding rounding to float: split the digits up into two parameters, each forming an integer not larger than that limit, and avoiding leading zeros in the second parameter. If the latter requires a larger first parameter: for every trailing 0 it can be a factor 2 larger then mentioned (see also Help:Calculation accuracy#Integers). To enter an integer smaller than -9,007,199,254,740,992 explicitly, do the same with a minus sign before each part.

Thus, when applying #expr, if the value is in the range of integer-type numbers the exact result is produced. The reason for not applying #expr in this template is:

  • for integer-type numbers: in subsequent use in another expression the value would be rounded to float
  • for floats: they would be rounded to 14 digits

Examples

[edit]

Integer-type expressions (unchanged):

  • {{numf|{{trunc|trunc(2^63-1024)+trunc1023}}}} → 9,223,372,036,854,776,000

Explicit integer values in the range of type integer:

  • {{trunc|123|4567890123456789}} → trunc((trunc(123))e trunc(15+1)+trunc4567890123456789)
  • {{trunc|1234|567890123456789}} → trunc((trunc(1234))e trunc(14+1)+trunc567890123456789)
  • {{trunc|123456|7890123456789}} → trunc((trunc(123456))e trunc(12+1)+trunc7890123456789)
  • {{trunc|1234567|890123456789}} → trunc((trunc(1234567))e trunc(11+1)+trunc890123456789)
  • {{trunc|12345678|90123456789}} → trunc((trunc(12345678))e trunc(10+1)+trunc90123456789)
  • {{trunc|1234567890|123456789}} → trunc((trunc(1234567890))e trunc(8+1)+trunc123456789)
  • {{trunc|12345678901|23456789}} → trunc((trunc(12345678901))e trunc(7+1)+trunc23456789)
  • {{trunc|123456789012|3456789}} → trunc((trunc(123456789012))e trunc(6+1)+trunc3456789)
  • {{trunc|1234567890123|456789}} → trunc((trunc(1234567890123))e trunc(5+1)+trunc456789)
  • {{trunc|12345678901234|56789}} → trunc((trunc(12345678901234))e trunc(4+1)+trunc56789)
  • {{trunc|123456789012345|6789}} → trunc((trunc(123456789012345))e trunc(3+1)+trunc6789)
  • {{trunc|1234567890123456|789}} → trunc((trunc(1234567890123456))e trunc(2+1)+trunc789)
  • {{numf|{{trunc|123|4567890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|1234|567890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|123456|7890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|1234567|890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|12345678|90123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|1234567890|123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|12345678901|23456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|123456789012|3456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|1234567890123|456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|12345678901234|56789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|123456789012345|6789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|1234567890123456|789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|9e17|1}}}} → 9,000,000,000,000,000,000 (to avoid leading zeros in the second parameter, the first parameter is almost 100 times as large as 9,007,199,254,740,992. However, for every trailing 0 it can be a factor 2 larger then this number; in this case there are 17 trailing zeros; since the factor 100 is less than 2^17 the first parameter is exactly representable as float).
  • {{numf|{{trunc|-123|-4567890123456789}}}} → −1,234,567,890,123,456,800

Float expressions with integer values in the range of type integer (the template converts them to type integer):

  • {{#expr:{{trunc|-2^63}}}} → -9223372036854775808

Float expressions with a non-integer value (the template applies the mathematical trunc function and returns a result of type integer)::

  • {{numf|{{trunc|98.7}}}} → 98
  • {{numf|{{trunc|20/3}}}} → 6
  • {{numf|{{trunc|1e6/3}}}} → 333,333

Floats outside the range of type integer (unchanged):

  • {{numfh|{{trunc|123.45e20}}}} → 12,345,000,000,000,000,000,000 (ca. 1.2e22) 1.4e9ca52eb182ahex*2^73
Compare the operator trunc:
  • {{numf|trunc123.45e20}} → 4,128,214,688,309,707,000

A leading zero in the second parameter is ignored:

  • {{numf|{{trunc|123456789|0123456789}}}} → 123,456,789,123,456,780

If one of the integers is too large, it is rounded in the conversion to float, making the result inaccurate:

  • {{numf|{{trunc|1|234567890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|12|34567890123456789}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|12345678901234567|89}}}} → 1,234,567,890,123,456,800
  • {{numf|{{trunc|123456789012345678|9}}}} → 1,234,567,890,123,456,800