Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mod() function Bug?
Message
From
22/09/2006 21:27:08
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
22/09/2006 10:30:37
Calvin Smith
Wayne Reaves Computer Systems
Macon, Georgia, United States
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01156435
Message ID:
01156662
Views:
17
>The mod command gives stange results when done this way:
>Mod(-13,4). I would expect this to return either 1 or -1 but it returns 3.
>Visual Basic return -1 for this command but Ruby return the same thing as VFP. Can someone explain this to me.... Thanks.

You can visualize this as follows. (Instead of mod(13, 4), I will write 13 % 4, which is also valid in VFP.)

To calculate 13 % 4, 4 is repeatedly subtracted, until you get a result such that 0 < = x < 4.

To calculate -13 % 4, 4 is repeatedly added, until (once again) you get a result such that 0 < = x < 4.

Of course, VFP will optimize this - it will not subtract or add the same number millions of times.

Also, note that you can use the modulus with non-integers. For example, you can normalize an angle (in radians) to the range 0 - 2pi:
? angle_in_radias % (2 * pi())
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Reply
Map
View

Click here to load this message in the networking platform