User Tools

Site Tools


4rpl:commands:mod2

Index
Arithmetic

mod2

<-a <-b mod2 ->result

Description

Perform integer division and returns the value (modulo) of the difference.
Works differently with negative numbers than mod, if the result of the modulo operation is negative, the divisor will be added to the result.
Performs this calculation: result = a % b; if (result < 0) result += b;

When using a positive number as divisor, mod2 is useful for 'wrapping' a value between 0 and the divisor.
For example: -50 mod(360) will normally return -50, but using mod2 it will return 310.

If the divisor is a negative number and the result of the normal modulo operation is too, then the divisor still gets added.
This means -50 mod2(-360) will return -410 rather than -50

Examples

-50 360 mod2 Trace # prints 310

See Also

Index

4rpl/commands/mod2.txt · Last modified: 2022/05/18 15:31 by Karsten75