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 0.299999999999999990 . di %21.18f .1 0.100000000000000010
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 2.999999999999999600
with
. di %21.18f 3 3.000000000000000000
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.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.