Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Mod of negative values

 
Post new topic   Reply to topic    Forum Index -> ZBasic IDE
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 Reply with quote

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: Reply with quote

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: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic IDE Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group