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?
This is a error??
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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.
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!
Ted
Code: Select all
IF x + CINT(x ^ 2) = 30 THEN PRINT x '
NOTE: It may show 25 in your PRINT at the end however. Don't ask me why!
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
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
Ok, thanks!!!
Ok !!!
thank you very much, Ted
thank you very much, Ted
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
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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