Page 1 of 1

This is a error??

Posted: Sat Dec 13, 2008 4:45 pm
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?

Posted: Sat Dec 13, 2008 6:11 pm
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

Ok, thanks!!!

Posted: Sat Dec 13, 2008 6:35 pm
by lrcvs
Ok !!!

thank you very much, Ted

Posted: Sat Dec 13, 2008 6:50 pm
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

Posted: Sat Dec 13, 2008 7:58 pm
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