## divisible by 2

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

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico
Ralph wrote:Hello,, Moneo!

Here is another quirk I find in QuickBASIC 4.5.
I ran the code in G-W BASIC, and got Odd. 0, ODD, odd, 0, A is odd.
I ran the code in QuickBASIC and got Odd, -1.734723E-18, EVEN, even, -1.734723E-18, A is odd.

So, the value that qb returns for 5 ^ 2 is not truly the integer 25, since the above shows that the difference is not 0.

It seems to me that your use of AND to detect odd or even would not be proper in qb, since further use of 5 ^ 2 as an integer would not be true in qb.

To truly get the exact integer 25, I find that Ted's answer of assigning a variable to it, seems to be the only way to go.

[code[CLS
IF F ^ 2 AND 1 THEN PRINT "Odd" ELSE PRINT "Even"
PRINT 5 ^ 2 - 25
IF 5 ^ 2 - 25 = 0 THEN PRINT "ODD" ELSE PRINT "EVEN"
IF INT(5 ^ 2) - 25 = 0 THE PRINT "odd" ELSE PRINT "even"
A = 5 ^ 2
IF A = 25 = 0 THEN PRINT "A is odd" ELSE PRINT "A is even"
Hi Ralph,

In my solution above for testing for odd or even numbers, I specifically said "assuming that the number is an integer." Your code, without any DEFtype statement, will be working with the default type of Single.

Also, we already know that expressions with exponentiation are computed using floating point, and the result may give you an answer in floating point notation. This is especially true when you print the expression out without storing it first into a variable.

I don't understand why you often chose to use expressions with exponentiation, like 5 ^ 2 - 25, for testing code that has nothing to do with exponentiation.

My odd/even solution is based on using the AND operator, and yet of the 4 statements of yours that determine odd/even, only the first one uses the AND operator. What was your thinking here?

Regards..... Moneo

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico
Ralph wrote:Hello,, Moneo!

Here is another quirk I find in QuickBASIC 4.5.
I ran the code in G-W BASIC, and got Odd. 0, ODD, odd, 0, A is odd.
I ran the code in QuickBASIC and got Odd, -1.734723E-18, EVEN, even, -1.734723E-18, A is odd.

So, the value that qb returns for 5 ^ 2 is not truly the integer 25, since the above shows that the difference is not 0.

It seems to me that your use of AND to detect odd or even would not be proper in qb, since further use of 5 ^ 2 as an integer would not be true in qb.

To truly get the exact integer 25, I find that Ted's answer of assigning a variable to it, seems to be the only way to go.

[code[CLS
IF F ^ 2 AND 1 THEN PRINT "Odd" ELSE PRINT "Even"
PRINT 5 ^ 2 - 25
IF 5 ^ 2 - 25 = 0 THEN PRINT "ODD" ELSE PRINT "EVEN"
IF INT(5 ^ 2) - 25 = 0 THE PRINT "odd" ELSE PRINT "even"
A = 5 ^ 2
IF A = 25 = 0 THEN PRINT "A is odd" ELSE PRINT "A is even"
Hi Ralph,

In my solution above for testing for odd or even numbers, I specifically said "assuming that the number is an integer." Your code, without any DEFtype statement, will be working with the default type of Single.

Also, we already know that expressions with exponentiation are computed using floating point, and the result may give you an answer in floating point notation. This is especially true when you print the expression out without storing it first into a variable.

I don't understand why you often chose to use expressions with exponentiation, like 5 ^ 2 - 25, for testing code that has nothing to do with exponentiation.

My odd/even solution is based on using the AND operator, and yet of the 4 statements of yours that determine odd/even, only the first one uses the AND operator. What was your thinking here?

Regards..... Moneo

burger2227
Veteran
Posts: 2429
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
I would really like to see a program that does not use integers and can tell me if the Single or Double value is divisible by 2.
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

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas
Hi, Moneo:
The reason I used AND in my code was to show that the result obtained by using AND, as well as a number we know has a small remainder in QuickBASIC (in ALL qBASICs?). For the case of testing integers, AND works fine, giving the correct mathematical result. However, it does not give the correct QuickBASIC result, which has a remainder.
My use of 5^2 is on purpose, because it has a remainder in qb. So, I use it to show that we have to be careful how we use derived values, which may not be exact in qb.

Ted:
burger2227 wrote:I would really like to see a program that does not use integers and can tell me if the Single or Double value is divisible by 2.
Dos this program not satisfy your question?:

Code: Select all

``````again:
CLS
PRINT " To determing if a number is divisible by 2, enter number:"
LOCATE , 2
LINE INPUT num\$
num = VAL(num\$)

IF INT(num) <> num THEN
decimals = LEN(num\$) - INSTR(num\$, ".")
numINT = VAL(num\$) * 10 ^ decimals
ELSE
numINT = num
END IF

LOCATE CSRLIN - 1
IF numINT AND 1 THEN
PRINT " "; num\$; " is NOT divisible by 2 "
ELSE
PRINT " "; num\$; " is divisible by 2"
END IF``````
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

burger2227
Veteran
Posts: 2429
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
Back to the original post question, why is my MOD 2 suggestion so wrong?

I have learned that arguing with Moneo is a waste of time anyhow! How he can figure out what a number is, is beyond me. He does not use the QB IDE at all.
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

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas
Ted, your MOD solution is good for integers; so is AND; so are others. I've said it befor, and I will now repeat it. Ted, your knowledge in electronics and in qb is very much appreciated by me and, I'm sure, by many others. Why do you have to ruin it by picking on people? I know that it amuses you, but, not others. PLEASE change, and become a "good guy".
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

burger2227
Veteran
Posts: 2429
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
Your right Ralph, but I have already discussed Singles, Doubles, and Rounding till I was blue in the face. It is hard for him to grasp unless he used the IDE once in a while. But he has stubbornly resisted for years!
He compiles Everything...........

Now to your decimal number code: You can just split the string number at the INSTR(number\$, ".") and use both sides of it as strings. Then just use VAL and MOD 2 also. But what is the point of finding even decimal point values anyhow? The point accuracy in QB is ALWAYS questionable while Integers don't have any problems!

Ted