Modulus
Posted: 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)'.
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)'.