KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Tips to use the modulo operator correctly in 4D.
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: July 22, 2025

The modulo operator is very common and useful to obtain the remainder of a division operation.
At first glance, the following code might look correct:

C_REAL($a)
C_REAL($b)
C_REAL($c)

$a:=200000000001
$b:=10^10

$c:=$a%$b

While it might work for other data types, including integers and long integers, it might fail when using real values, as indicated in the 4D documentation. "The modulo "%" operator returns valid results with Integer and Long Integer expressions. To calculate the modulo of real values, you must use the Mod command."

In this case, the modulo "%" operator should be replaced by the Mod command:

$c:=Mod($a; $b)