Remainder()

From TinyMUX
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Description

FUNCTION: remainder(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 smallest integer that is greater 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 -1 by this definition instead of -2. idiv() would return -1. floordiv() would return -2, and so, the remainder() function properly goes with the idiv() function:


idiv(x,y)*y + remainder(x,y) ==> x


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

remainder() is usually the way the '%' operator in the C programming language is defined.

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

Examples

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

Server Differences

Remainder() 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).