Qbasicnews.com
Quick problem... - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Quick problem... (/thread-9300.html)

Pages: 1 2


Quick problem... - Rokkuman - 05-19-2006

Basically:

Code:
A = .5
PRINT A

Output:

"0"

So FB is rounding everything to a whole number, and it seems to be only specific to FB, as the same exact code will display ".5" in QB. Anyone know the problem? Thanks in advance.


Quick problem... - yetifoot - 05-19-2006

Yes, undefined variables are Integers in FB. In order to change to QB behaviour you can use..

Quote:DEFSNG A-Z

Code:
DEFSNG A-Z

A = .5
PRINT A


The reason for this (I'm told) is that Integers are generally faster to work with than Singles especially on older machines.


Quick problem... - Rokkuman - 05-19-2006

Thanks, that helped me to get the program to recognize the variable as that decimal number, however, I've run into another problem.

Code:
DIM a AS SINGLE
DIM b AS SINGLE
DIM r as SINGLE

a = 512
b = 30
R = b \ a

PRINT a
PRINT b
print r
PRINT 30 \ 512

SLEEP

The output for this program is that apparently, 512 \ 30 is 0, which is wrong. Punching it into the calculator yields "0.057692307692307692307692307692308", so obviously both FB and QB are doing something to limit decimals like this. Is there any way to maybe get the true decimal result to the nearest thousandth? Thanks again.


Quick problem... - Zack - 05-20-2006

Hey PJ,
\ signifies integer division, so the program will round the result (since 30/512 is less than 0, it will round to 0).
Use /.
Code:
DIM a AS SINGLE
DIM b AS SINGLE
DIM r as SINGLE

a = 512
b = 30
R = b / a

PRINT a
PRINT b
print r
PRINT 30 / 512

SLEEP



Quick problem... - anarky - 05-22-2006

I always use "/". Is "\" faster when dividing integers? Like 50 \ 2, which would return 25, an integer?


Quick problem... - yetifoot - 05-22-2006

Yes it is faster. However you have to remember that it works differently to how you might expect.

If dividing ie: a \ b

it returns how many whole 'b's fit into 'a'. It doesnt round up.

like 25 \ 2 = 12


Quick problem... - TheAdventMaster - 05-22-2006

I thought FB rounded down anyways, so it works exactly like I expect it to:

Code:
print 3 / 4

print int(3 / 4)
sleep



Quick problem... - yetifoot - 05-22-2006

I guess it depends which way you look at it.

in the example i gave 25 \ 2, some people might expect to get 13, as 12.5 would normally round to 13


Quick problem... - stylin - 05-22-2006

int is like floor. If you want rounding, you need a conversion:

Code:
    ? "int(-0.9) = " ; int(-0.9)
    ? "int(-0.1) = " ; int(-0.1)
    ? "int(0.1) = " ; int(0.1)
    ? "int(0.9) = " ; int(0.9)

    ? "cint(-0.9) = " ; cint(-0.9)
    ? "cint(-0.1) = " ; cint(-0.1)
    ? "cint(0.1) = " ; cint(0.1)
    ? "cint(0.9) = " ; cint(0.9)



Quick problem... - Anonymous - 05-22-2006

add .5 and floor it? thats the convention i believe