Help:Mod, round, floor, ceil, trunc

From SacredWiki
Revision as of 22:02, 10 October 2013 by Schot (talk | contribs) (1 revision: Updating our Help Pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

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:

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

{{#expr: 30 mod 7}} gives 2 [1]
{{#expr: -8 mod -3}} gives -2 [2]
{{#expr: -8 mod +3}} gives -2 [3]
{{#expr: 8 mod 2.7}} gives 0 [4] (elsewhere 2.6)
{{#expr: 8 mod 3.2}} gives 2 [5] (elsewhere 1.6)
{{#expr: 8.9 mod 3}} gives 2 [6] (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:

Former errors

Errors due to 32 bits servers do not occur anymore on Wikimedia wikis, since their servers are all 64 bits now. As a result we get the following correct results:

  • Template:For
  • {{numf|123mod(trunc(2^63-1024)+trunc1023)}} → 123

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:

  • {{numf|floor (trunc1e17+trunc1)}} → 100,000,000,000,000,000
  • {{numf|ceil (trunc1e17+trunc1)}} → 100,000,000,000,000,000

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

  • {{numf|{{floor|trunc1e17+trunc1}}}}Expression error: Unrecognized punctuation character "[".
  • {{numf|{{ceil|trunc1e17+trunc1}}}}Expression error: Unrecognized punctuation character "[".

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

  • {{#expr:1.234 + 1.234 round 1 + 1}} gives 2.47 [7]

(first additions, then round)

  • {{#expr:3 * 4 mod 10 * 10}} gives 20 [8]

(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:
{{#expr:23+45 mod 10}} gives 28 [9]
Instead one can write:
{{#expr:23 + 45 mod10}} gives 28 [10]
or use parentheses:
{{#expr:23 + (45 mod 10)}} gives 28 [11]

See also

<references group=""></references>

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · mw: · b: · s: · mw:Manual · Google
Versions of this help page (for other languages see further)
Meta · Wikinews · Wikipediahttp://en.wikipedia.org/index.php/Help:Mod,_round,_floor,_ceil,_trunc · Wikiquote · Wiktionary
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Stop words · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URLs · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Testing · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Embed page
Others
Special characters · Renaming (moving) a page · Talk page · Signatures · Sandbox · Legal issues for editors