Home  /  Resources & support  /  FAQs  /  Results of the mod(x,y) function
Note: This FAQ is based on a question and answers that appeared on Statalist.

Why does the mod(x,y) function sometimes give puzzling results? Why is mod(0.3,0.1) not equal to 0?

Title   Results of the mod(x,y) function
Author Nicholas J. Cox, Durham University, UK
Thomas J. Steichen

The mod(x,y) function (see [FN] functions) is equivalent to x − y * floor(x/y). Here floor() returns the largest integer not greater than its argument so that floor(2) = 2, floor(2.3) = 2, floor(−2) = −2, and floor(−2.3) = −3. In other words, it is the remainder on dividing x by y. It is obvious that 0.3 is a multiple of 0.1 and that the result should be 0, but given

        . display mod(0.3,0.1)

Stata shows 0.1.

Is this is a bug? Not really, but the result is unlikely to be what you want. It arises because int(0.3/0.1) = 2 to machine precision, and not 3, as expected from ordinary arithmetic. Hence, 0.1 is shown as the remainder or modulus.

Let us explain in more detail. Numbers like 0.3 and 0.1 cannot be held as exact binary equivalents; see [U] 13.12 Precision and problems therein. To show this, we will use %21.18f as a display format, which lets us see the underlying values with pretty much the same precision as Stata. (Stata also provides a special %21x format that shows the exact value in a special hexadecimal format, which you may wish to explore. This format was first available late in the product cycle of Stata 6.0. See also [U] 12.2 Numbers and [U] 12.5.1 Numeric formats.)

        . di %21.18f .3
        . di %21.18f .1

We are using essentially every bit here to get the closest machine decimal approximation we can to the true decimals 0.3 and 0.1. Clearly, neither number is represented exactly in the machine.

If we compare

        . di %21.18f .3/.1


        . di %21.18f 3

it is also clear that, in machine decimal, 3 is not equal to 0.3/0.1. This occurs because 3 can be represented accurately while 0.3/0.1 is just a smidgen smaller due to the approximations required to represent 0.3 and 0.1. Clearly, the integer portion of 0.3/0.1 = 2.999999999999999600 is 2, with the consequence that mod(0.3,0.1) is shown as 0.1 (more precisely, it is 0.099999999999999978 in machine decimal, but Stata's default display format rounds it to 0.1).

Users may get better results using float( ) and round( ), but perhaps the best advice is cautionary: it is safest not to try precision work using small fractional arguments to mod( ). You may be better advised to work at an equivalent problem using integers.

If you seek more detail, type search precision within Stata to identify various resources. The blog posts by William Gould are particularly recommended.