Page 1 of 1

Why not the same result?

Posted: Sat Sep 15, 2007 5:05 pm
by nyfiken
I?m confused about these few lines. Does anyone know why the number
20070501 becomes 20070500 in the last kolumn?

Code: Select all

CLS

number$ = "20070501"
PRINT number$;
day$ = LEFT$(number$, 8)
PRINT USING " ########"; VAL(day$);
day = VAL(LEFT$(number$, 8))
PRINT USING " ########"; day
The output is:
20070501 20070501 20070500



If I change number$ to "20070502" then the three kolums will be the same, 20070502 20070502 20070502.

:?:
________
HOT BOX VAPORIZER

Posted: Sat Sep 15, 2007 6:25 pm
by Anonymous
Hi,

Because you are using such a large number when you day = VAL(LEFT$(number$, 8)) qbasic makes day an integer which only goes up to 32768, so if you dim day as a long it will work.

Code: Select all

CLS

number$ = "20070501"
PRINT number$;
day$ = LEFT$(number$, 8)
PRINT USING " ########"; VAL(day$);
dim day2 as long
day2 = VAL(LEFT$(number$, 8))
PRINT USING " ########"; day2 

Posted: Sat Sep 15, 2007 9:25 pm
by Mac
Nixon wrote:Because ...blah, blah
Good answer!!

Mac

Posted: Sun Sep 16, 2007 6:24 am
by nyfiken
Interesting, I thought QB was 100% safe regarding variables, that it generated some kind of oveflow error when the numbers were out of limit. Newertheless it's no problem if one knows about it. Thanks!
________
Bmw V5

Posted: Sun Sep 16, 2007 7:20 am
by Nodtveidt
It's a floating point conversion error. day is going to be SINGLE by default. VAL is irrespective of numeric type; it will return a value that fits into the data type you use with the function.

Try using PRINT day at the end of your program...you'll get an interesting result that confirms the conversion error. It's a fault with floating point values.

As a general rule, you should always DIM your variables before you use them, especially in QB where all variables are SINGLE by default.