Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick problem...
#1
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.
Reply
#2
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.
[Image: chav.gif]
Reply
#3
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.
Reply
#4
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.
Reply
#5
I always use "/". Is "\" faster when dividing integers? Like 50 \ 2, which would return 25, an integer?
Screwing with your reality since 1998.
Reply
#6
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.
[Image: chav.gif]
Reply
#7
I thought FB rounded down anyways, so it works exactly like I expect it to:

Code:
print 3 / 4

print int(3 / 4)
sleep
Reply
#8
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.
[Image: chav.gif]
Reply
#9
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:
Reply
#10
add .5 and floor it? thats the convention i believe
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)