Question about math and data types
Posted: Wed Jul 24, 2024 6:54 am
Hi
I was doing some experiments with math and floats and I found some things that looks inkonsistent to me, which makes some calculations impossible (as far as I understand).
I'd like to get your feedback on the below and a suggestion how I should do things differently, eventually.
What I would expect here would be (as in other languages):
1. If a variable is explicitly declared to be of a given type, it is created to be of that type, regardless how it is initialized.
2. The type of an expression is the "biggest" type of its operands (so 1/3.0 is a float expression and so it is a/10.0 even if a is an integer).
3. If a variable is not declared explicitly, either it is created with a default type (e.g. all variables are INTEGER) OR its type is determined by its initialization expression.
Thanks & Regards
Maxi
I was doing some experiments with math and floats and I found some things that looks inkonsistent to me, which makes some calculations impossible (as far as I understand).
I'd like to get your feedback on the below and a suggestion how I should do things differently, eventually.
What I would expect here would be (as in other languages):
1. If a variable is explicitly declared to be of a given type, it is created to be of that type, regardless how it is initialized.
2. The type of an expression is the "biggest" type of its operands (so 1/3.0 is a float expression and so it is a/10.0 even if a is an integer).
3. If a variable is not declared explicitly, either it is created with a default type (e.g. all variables are INTEGER) OR its type is determined by its initialization expression.
Thanks & Regards
Maxi
Code: Select all
a=10
b=a/3
REM a and b are treated as integers and int math is used
REM This is as expected
PRINT "a,b "; a,b
DIM c,d AS FLOAT
c=10
d=c/3
REM c seems to be treated as int while d seems to be float
REM Still, int math is used (d=3.0 and not 3.3333).
REM This is IMHO not as it should be as data type is explicitly declared,
REM regardless whether c and d are assigned to a int and float math should be used
PRINT "c,d "; c,d
e=10.0
f=e/3.0
REM e and f are treated as float and float math is used
REM This is as expected
PRINT "e,f "; e,f
REM So far, it seems type of variables is determined by the type
REM of their initialization value.
REM This makes DIM statement useless though (except for arrays)
g=10
h=g/3.0
REM g and h are treated as integers and int math is used
REM This is confusing to me, as I would expect h be float
REM (as it is assigned to a float expression).
REM It seems to be inconsistent with what happens above.
PRINT "g,h "; g,h
REM This raises an issue: If I have an expression where only int variables
REM are used, but I want that expression to be calculated using float math,
REM how can I do it?
REM In the example above I do want h to be 3.33333
i=10
DIM j AS FLOAT: j=i
j=j/3.0
DIM k AS FLOAT: k=i/3.0
REM This works for j but not for k
REM It seems this gives me a "workaround" for j
REM (which seems a bit inconsistent as it is initialized with an int, see c above)
REM but this means I need to "duplicate" my variables at each calculation
PRINT "i,j,k "; i,j,k