This is a error??

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
lrcvs
Veteran
Posts: 58
Joined: Mon Mar 10, 2008 9:28 am

This is a error??

Post by lrcvs »

CLS
FOR x = 1 TO 10

IF (x) + (x * x) = 30 THEN PRINT x ;" Ok!!"

IF (x) + (x ^ 2) = 30 THEN PRINT x '"Why this line does not work in QB?"

NEXT x
PRINT 5^2 ;" Ok!!"
END


Note: In FB, work well, why?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Because exponents return Floating Decimal point results. Normally a Double value. It will also do that with REAL values instead of variables! Try putting CINT around it.

Code: Select all

 IF x + CINT(x ^ 2) = 30 THEN PRINT x '
I gather it should print when X = 5. For larger values of X you can use CLNG.

NOTE: It may show 25 in your PRINT at the end however. Don't ask me why! :lol:

Ted
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
lrcvs
Veteran
Posts: 58
Joined: Mon Mar 10, 2008 9:28 am

Ok, thanks!!!

Post by lrcvs »

Ok !!!

thank you very much, Ted
roy
Veteran
Posts: 55
Joined: Sun Jul 29, 2007 2:39 pm
Location: London

Post by roy »

As Clippy says QB uses double precision , which I would class as a bug. The reason it prints the 25 correctly, is because it is printing in single precision. This removes any garbage at the end. Another way out would be to make x^2 into a variable as below.

CLS
FOR x = 1 TO 10
IF (x) + (x * x) = 30 THEN PRINT x;
a = x ^ 2
IF x + a = 30 THEN PRINT x
NEXT x
PRINT 5 ^ 2; " Ok!!"
END
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Good to know Roy! I never thought of PRINT doing that, but I must admit, I am actually making a demo on exponent return values and now I know why. What a coincidence.

Thanks! Keep up the good work!

Ted
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