Posts: 1,556
Threads: 298
Joined: Dec 2002
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.
Posts: 484
Threads: 14
Joined: Apr 2005
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.
EVEN MEN OF STEEL RUST.
Posts: 1,556
Threads: 298
Joined: Dec 2002
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.
Posts: 3,616
Threads: 287
Joined: Jan 2003
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
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 961
Threads: 81
Joined: Apr 2005
I always use "/". Is "\" faster when dividing integers? Like 50 \ 2, which would return 25, an integer?
Screwing with your reality since 1998.
Posts: 484
Threads: 14
Joined: Apr 2005
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
EVEN MEN OF STEEL RUST.
Posts: 476
Threads: 35
Joined: May 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
Posts: 484
Threads: 14
Joined: Apr 2005
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
EVEN MEN OF STEEL RUST.
Posts: 324
Threads: 12
Joined: Nov 2005
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)
stylin:
add .5 and floor it? thats the convention i believe