() Mod () | |

() mod () | |

Category |
Operators |

Type |
Reporters |

The **() Mod ()** block ("mod" is short for "modulo") is an Operators block and a reporter block. It reports the remainder when the first input is divided by the second. For example, when 10 is put in the first input and 3 in the second, the block will report 1; 10 divided by 3 gives a remainder of 1.

() Mod () supposes the quotient (result of division) is always rounded down, even if it is negative. For example, -10 mod 3 = 2, not -1, because the quotient -10/3 is rounded down to -4, giving a positive remainder.^{[note 1]}

## Example Uses

If a project is doing divisibility tests, the () Mod () block can be of use.

Some common uses for the () Mod () block:

- Checking if two numbers divide without a remainder

if <((a) mod (b)) = [0]> then say [a is divisible by b] else say [a is not divisible by b] end

- Checking if a number is a whole number

if <((a) mod (1)) = [0]> then say [a is a whole number] else say [a is not a whole number] end

- Checking if numbers are odd or even

if <((a) mod (2)) = [0]> then say [a is an even number] else if <((a) mod (1)) = [0]> then say [a is an odd number] else say [a is not an integer] end end

- Repeatedly iterating through a list:

when gf clicked set [x v] to [0] forever change [x v] by (1) say (item (x) of [list v]) set [x v] to ((x) mod (length of [list v])) end

- Reusing background-sprites when scrolling

when gf clicked forever set x to (((x position) + (240)) mod (480)) end

## Workaround

*Main article: List of Block Workarounds*

Because the remainder of a division is the dividend multiplied by the fractional part of the quotient, the block can be replicated with the following code (a and b represent the inputs):

((a) - ((b) * ([floor v] of ((a) / (b)))))

If the result wanted is the remainder supposing the quotient is rounded towards 0, a Scratcher can either take the result of () Mod () block and subtract the dividend once, as so:

if <((a) / (b)) < [0]> then set [r v] to (((a) mod (b)) - (b)) else set [r v] to ((a) mod (b)) end

or round the quotient towards 0 and compute the remainder from there:

set [q v] to ((a) / (b)) if <(q) > [0]> then set [q v] to ([floor v] of (q)) else set [q v] to ([ceiling v] of (q)) end set [r v] to ((a) - ((b) * (q)))

## See Also

## Notes

() + ()
• () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks... |