# Mod()

Jump to navigation Jump to search

## Description

FUNCTION: mod(integer1, integer2)

Returns the integer remainder from dividing integer1 by integer2.

However, integer division for the case where either integer1 or integer2 is negative is defined in a specific way -- by choosing the largest integer that is less than or equal to the algebraic quotient. If integer1 and integer2 are the same sign, then mod() and remainder() are equivalent.

For example, division of -9 by 5 would give -2 by this definition instead of -1. idiv() would return -1. floordiv() would return -2, and so, the mod() function properly goes with the floordiv() function:

floordiv(x,y)*y + mod(x,y) ==> x

For positive y, mod(x,y) always returns a positive number less than y. For negative y, mod(x,y) always returns a negative number greater than y.

mod() is the more 'mathy' definition of a modulus as defined by the following:

x mod y is defined as x - y*floor(x/y) where x,y, every operation is over real numbers.

Related Topics: floordiv(), fmod(), iadd(), idiv(), imul(), isub(), remainder().

> say mod(-9,5)
You say "1"
> say mod(-9,-5)
You say "-4"
> say mod(17,3)
You say "2"
> say mod(18,3)
You say "0"

## Server Differences

Modulo() is almost the same across TinyMUX, PennMUSH, and TinyMUSH except that TinyMUX supports 64-bits integers on both 32-bit and 64-bit platforms while the other two support the native integer size (32-bit or 64-bit depending on the platform).

Another potential confusion is that on TinyMUX, the built-in function is named mod(), and modulo() is an alias. On PennMUSH, the built-in function is modulo(), and mod() is the alias. While, on TinyMUSH, the built-in function is modulo() but mod() is an alias for a different-but-related function, remainder().

Therefore, use modulo() to obtain the same behavior across all three servers.