|
|
| Author |
Message |
GTBecker
Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral
|
|
Posted: 12 November 2009, 1:23 AM Post subject: Mod of negative values |
|
|
Paul's last message spurred a question I failed to ask some time ago.
Consider:
| Code: | Sub Main()
console.writeline(cstr(-90.0 mod 360.0))
End Sub |
A practical application of this is counterclockwise rotation of a shaft that points to a degree wheel. -90 degrees from 0.0/+360.0 is +270.0 degrees in practice. What is the rationale of yielding -90.0 for (-90.0 mod 360.0)?
Tom |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX
|
|
Posted: 12 November 2009, 2:10 AM Post subject: |
|
|
| Depending on the programming language concerned the modulus operator takes either the sign of the divisor or the sign of the dividend. As ZBasic is derived from Visual Basic, it uses the same scheme as Visual Basic which is to use the sign of the dividend. Therefore taking the modulus of any negative number in ZBasic results in a negative result. |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 12 November 2009, 3:20 AM Post subject: |
|
|
| mikep wrote: | | As ZBasic is derived from Visual Basic, it uses the same scheme as Visual Basic which is to use the sign of the dividend. | Just so. It is unfortunate that BasicX, which also derives from Visual Basic, returns a positive result. When we were implementing ZBasic, we had to choose whether it was more important to be compatible with VB or BasicX. In this particular case we opted for VB compatibility and noted the difference in the Compatibility Issues section of the ZBasic Reference Manual.
Regarding your specific use case, you can get a result in the 0-360 range using the idiom below. | Code: | | result = ((angle Mod 360.0) + 360.0) Mod 360.0 |
|
|
| Back to top |
|
 |
|