M:Help:Mod, round, floor, ceil, trunc

From OODA WIKI

Template:H:h

The MediaWiki extension ParserFunctions enables users to perform simple mathematical computations; #expr and #ifexpr allow various discontinuous functions.

Uniform w.r.t. 0, non-strict monotonic:[1]

  • ceil
  • floor

Symmetric w.r.t. 0, on each side of 0 non-strict monotonic:

  • trunc
  • round

Otherwise related to 0, on each side of 0 periodic:

  • mod

See also Help:Mod, round, floor, ceil, trunc/table.

Trunc

Trunc converts a float to a number of integer type by truncation of the fraction. For 2^63 <= x <= 2^64 we get x - 2^64, for larger x we get 0; for x < -2^63 we get -2^63.

For example:

  • {{#expr:trunc(-2*2^63-2^12)}} → -4096
  • {{#expr:trunc(-2*2^63+2^12)}} → 4096
  • {{#expr:trunc(-1*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(-1*2^63+2^12)}} → -9223372036854771712
  • {{#expr:trunc(0*2^63-2^12)}} → -4096
  • {{#expr:trunc(0*2^63+2^12)}} → 4096
  • {{#expr:trunc(1*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(1*2^63+2^12)}} → -9223372036854771712
  • {{#expr:trunc(2*2^63-2^12)}} → -4096
  • {{#expr:trunc(2*2^63+2^12)}} → 4096
  • {{#expr:trunc(2^64+1024)}} → 0
  • {{#expr:trunc(3*2^63-2^12)}} → 9223372036854771712
  • {{#expr:trunc(3*2^63+2^12)}} → -9223372036854771712

Conversion to a number of integer type is useful because the result, and subsequent results of type integer, are displayed exactly, instead of being rounded to 14 digits.

x = trunc x if and only if x is of type integer or a float with a value that a number of type integer also can have.

Mod

mod (modulo, PHP operator %)[2]: if a is nonnegative, a mod b is the remainder by division after truncating both operands to an integer (more accurately: apply the trunc function), while (-a) mod b = - ( a mod b) and a mod (-b) = a mod b.[3]

Template:Evaldemo
Template:Evaldemo
Template:Evaldemo
Template:Evaldemo (elsewhere 2.6)
Template:Evaldemo (elsewhere 1.6)
Template:Evaldemo (elsewhere 2.9)

To get a positive mod even for a negative number, use "(p mod q + q) mod q" instead of "p mod q".

Rounding to a float before applying mod can be avoided by constructing a type-integer form of the number:

  • {{#expr:(1e16+1)mod3}} → 1
  • {{#expr:(trunc1e16+trunc1)mod3}} → 2

For large integers there used to be errors, Template:Modint was used as an alternative.

Details

p mod 0 gives an error message (produced by MediaWiki, not php operator %):

  • {{#expr:-27mod0}}Division by zero.

Otherwise php operator % is applied. This involves applying the trunc function to the arguments first. The result of p % 0 is the empty string; this applies for 0 < |q| < 1 and q >= 2^64:

Round

See Help:Round.

Floor and ceil

When the functions floor and ceil are applied to an integer-type number, this number is first rounded to float (in the general way, not specifically downward for floor or upward for ceil) before applying the mathematical function:

Use Template:Floor and Template:Ceil to get in such a case the integer-type expression for the exact result:

To check whether the internal result of an expression x is mathematically an integer one can simply test "(x) = floor (x)", or similarly with ceil (not with trunc because for large floats we would get false negatives, and not with round0, because for odd numbers between 2^52 and 2^53 we would get false negatives).

Safety margins

To round an integer x down to a multiple of 7 we can do one of the following:

  • 7*((x-3)/7 round 0)
  • 7*floor((x+.5)/7)
  • 7*ceil((x-6.5)/7)

and to round x to the nearest multiple of 7 we can do one of the following:

  • 7*((x/7) round 0)
  • 7*floor((x+3.5)/7)
  • 7*ceil((x-3.5)/7)

In these cases the position with respect to 0 (the sign of x) is not important, even for round, because the value to be rounded is never halfway between integers. All methods provide the same generous safety margin of .5 in x, or 1/14 after division by 7, for rounding errors.

Note that for arbitrary real x the difference between the two problems corresponds to a shift in x of 3.5, while here, with integers x, the shift is 3.

Precedence

(first additions, then round)

(mod and multiplication have equal precedence, evaluation from left to right)

When using spaces where there is precedence, the layout of the expression may be confusing:
Template:Evaldemo
Instead one can write:
Template:Evaldemo
or use parentheses:
Template:Evaldemo

See also

  1. Estimation of a discrete monotone distribution
  2. https://www.w3schools.com/php/php_operators.asp
  3. mod with non-integer arguments is different from all programming languages, see bugzilla:6068 (marked as won't fix).

Template:H:f