Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

mod_alt

By Daniel · January 9, 2009

GML's modulo operator is implemented in such a way that the result always has the same sign as the dividend. For example, (-7 mod 2) evaluates to -1. This script implements the modulo operator in such a way that the result shares the sign of the divisor, which is sometimes more useful.

/* mod_alt(dividend, divisor)
 *
 * GML's modulo operator is implemented in such a way that the result always
 * has the same sign as the dividend. For example, (-7 mod 2) evaluates to
 * -1. This script implements the modulo operator in such a way that the
 * result shares the sign of the divisor, which is sometimes more useful.
 * See the table for some comparisons.
 *
 *  ---------------------------------------------
 * | dividend | divisor | standard mod | mod_alt |
 * | ------------------------------------------- |
 * | 7        | 2       | 1            | 1       |
 * | -7       | 2       | -1           | 1       |
 * | 7        | -2      | 1            | -1      |
 * | -7       | -2      | -1           | -1      |
 *  ---------------------------------------------
 */

return ((argument0 mod argument1) + argument1) mod argument1;

Categories: Mathematics

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement