Modulus

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
User avatar
DDastardly71
Jr. Member
Posts:15
Joined:Sun Jan 20, 2008 11:25 pm
Modulus

Post by DDastardly71 » Mon Aug 17, 2009 10:51 pm

I am trying to do a simple math using Modulus on large double-precision
numbers and I'm encountering an error.

I'm trying to convert an IP address to an IP number and I'm getting an
error on the last part. It works fine on paper but not in code.


IP_Address To IP_Number
w = 202
x = 186
y = 13
z = 4

IP_Number = (w * (256 ^ 3)) + (x * (256 ^ 2)) + (y * 256) + z


IP_Number to IP_Address

w = int( IP_Number / (256 ^ 3) ) MOD 256
x = int( IP_Number / (256 ^ 2) ) MOD 256
y = int( IP_Number / 256 ) MOD 256 <-- Error
z = int( IP_Number ) MOD 256 <-- Error


According to Microsoft MOD will "round real values to integers, if required".
To get the correct value for 'z' I had to write a modified function for
MOD but that only works half of the time.

Using the MOD on a very large double-precision number causes an error.
I've tested it and a double-precision number can handle numbers as high as (256 ^ 140)'.

User avatar
BurgerBytes
Jr. Member
Posts:22
Joined:Thu Aug 06, 2009 7:44 pm
Location:Pittsburgh, PA, United States

Post by BurgerBytes » Tue Aug 18, 2009 1:11 am

Answered at Pete's Site.
Last edited by BurgerBytes on Mon Aug 24, 2009 8:59 pm, edited 1 time in total.
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip

EkriirkE
Newbie
Posts:6
Joined:Sat Aug 06, 2016 5:11 am
Location:SFBA, CA
Contact:

Re: Modulus

Post by EkriirkE » Sun Aug 07, 2016 2:53 am

Use CLNG() instead of INT()
Alternatively, try bit masking to eliminate remainders and their rounding errors, e.g.


IP1=(IP AND &HFF&)
IP2=(IP AND &HFF00&) / &H0100&
IP3=(IP AND &HFF0000&) / &H010000&
IP4=(IP AND &HFF000000&) / &H01000000&
I take things apart. I break them. Then I put things that don't belong back inside them.

Post Reply