Modulus

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Modulus

Post by DDastardly71 »

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
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

MOD returns the integer remainder of a division. If it returns 0, then the number can be evenly divided by integer division. \ is used for real integer division, but it eliminates any remainder if there is any.

You don't even need the INT function in your calculations and you really don't want to use remainder division in this case.

Substitute the / divisions to \ integer division.

Why are you dividing by 256 for z? you never multiplied it by 256 in the first place.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Post by DDastardly71 »

I just copied the formula verbatim from the web site that I got it from but it would make sense to divide by something, otherwise, you will get whatever big number ip_number is.

This is the part where I'm confused. How to isolate the value of z.

Second question:

How do I use MOD on double-precision numbers?
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

How do I use MOD on double-precision numbers?
If you wanted to have a double modulus 5, you could do:

Code: Select all

dim myDouble as double
dim answer as integer

answer = cInt(myDouble) mod 5 'Casts myDouble to integer first
or

Code: Select all

dim myInteger as integer
dim myDouble as double
dim answer as integer

myInteger = myDouble

answer = myInteger mod 5
For any grievances posted above, I blame whoever is in charge . . .
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

How do you determine z?

Easy, just subtract the other variable values from the total IP value.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply