Help:Mod, round, floor, ceil, trunc

From The Final Challenge Wiki
Jump to navigation Jump to search

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:

  • 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:

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 %): 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.[1]

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:

For large integers there are errors (see below), use Template:Modint instead.

Details

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

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:

Help:Mod, round, floor, ceil, trunc/mod demo 1 illustrates that the rules after applying trunc, (-a) mod b = - ( a mod b) and a mod (-b) = a mod b, apply even in the case of erratic results, so we can concentrate on p mod q for integers (integer type or float with integer value) p and q, with 0 <= p <= 2^63 and 0 < q <= 2^63.

Errors

p mod q with q = 2^n-1 and q = 2^n+1 for n >= 32 always give zero:

For the important special case that q is a power of 10 no errors have been observed:

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. mod with non-integer arguments is different from all programming languages, see bugzilla:6068 (marked as won't fix).

Template:H:f