value1 Mod value2
Parameters
|
value1 - the number being divided value2 - the number to divide by |
Description
|
Returns the remainder left over after dividing one number by another. Mod is an operator, written between its two values: 7 Mod 3 is 1. It binds as tightly as * and /, so a+b Mod c means a+(b Mod c). Its most common job in a game is wrapping. angle = angle Mod 360 keeps a heading inside one turn no matter how long it has been spinning. frame = (frame+1) Mod frame_count cycles an animation without an If. index Mod width and index/width split a flat array position back into a tile column and row. Mod is also the "every nth" test: If (tick Mod 30)=0 fires something once a second at 30 frames per second. Two things to watch. The result takes the sign of the left-hand value, not the right: 7 Mod 3 is 1 but -7 Mod 3 is -1. So wrapping a value that can go negative needs a second nudge, such as ((a Mod 360)+360) Mod 360, or you will get negative angles back. And Mod works on floats as well as integers - 7.5 Mod 2.0 is 1.5 - so make at least one side a float when you want a fractional remainder, since two integers divide as integers. See also: Int, Float, Floor, Abs. |
Example
; Mod Example ; ----------- ; Mod returns the REMAINDER after dividing the left side by the right. Print "10 Mod 3 = "+(10 Mod 3)+" (10 = 3*3 with 1 left over)" Print "12 Mod 4 = "+(12 Mod 4)+" (divides exactly, no remainder)" Print "7 Mod 10 = "+(7 Mod 10)+" (7 is smaller than 10, so it IS the remainder)" Print "-7 Mod 3 = "+(-7 Mod 3)+" (the result takes the sign of the left side)" Print "" ; Mod keeps values cycling round a fixed range - great for clocks, ; wrapping angles and looping animation frames Print "9 o'clock plus 6 hours: (9+6) Mod 12 = "+((9+6) Mod 12) Print "" Print "Looping a 4-frame animation with tick Mod 4:" For tick=0 To 9 Write (tick Mod 4)+" " Next Print "" Print "" Print "Press any key to close the example" WaitKey End
Index