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)'.
Modulus
Moderators:Administrator, Global Moderator
- BurgerBytes
- Jr. Member
- Posts:22
- Joined:Thu Aug 06, 2009 7:44 pm
- Location:Pittsburgh, PA, United States
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
Re: Modulus
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&
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.