Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bug or missunderstood?
#1
Hello, if I run this program:
Code:
cls
dim a,b as ubyte
dim dw1,dw2 as uinteger
dim valor as uinteger
a=10
b=5
valor=a*b
print "valor:";valor
a=100
valor=a*b
print "valor:";valor
valor=10000
print "valor:";valor
print
valor=a*b
print "valor:";valor
print
dw1=10
dw2=5
valor=dw1*dw2
print "valor:";valor
dw1=100
valor=dw1*dw2
print "valor:";valor

the output in screen is:

valor:50
valor:244
valor:10000

valor:244

valor:50
valor:500

This is a bug?
Reply
#2
Confusedhock:
This definitely looks like a bug.
What compiler version are you using??
Reply
#3
Hello, the version is 1.4.0-s1967
Reply
#4
Haplo Wrote:Hello, the version is 1.4.0-s1967
Phew!! :roll:
It's not a bug (BTW the latest version is 1.4.0-s1968).

The problem is you are declaring a and b as ubyte, and ZX Basic does not typecast by default to Uinteger if the result is uinteger.
Your code:
Code:
cls
dim a,b as ubyte
dim dw1,dw2 as uinteger
b=5
a=100
valor=a*b
declares A and B as Ubyte. You should declare at least one of them as UInteger or use an explicit CAST.
Some compilers (e.g. most C Compilers) detects that 'valor' is Uinteger and will typecast A and B to Uinteger.
ZX Basic does not, for performance reasons (Z80 and 48k are so tiny... :roll: )
You can overcome this by doing an Explicit CAST. Try this:
Code:
cls
dim a,b as ubyte
dim dw1,dw2 as uinteger
b=5
a=100
valor=CAST(Uinteger, a) * b : REM Explicit CAST of a (or b) will convert all the expression to Uinteger
When using CAST( ), the expression (a) is converted ('casted') to UInteger. The compiler realizes that b is also Ubyte and "promotes" b also to Uinteger and peforms the multiplication.
Most C compilers do that automatically. ZXBasic needs you to explicit use CAST in one of the operands.
I'm thinking whether or not ZX Basic should follow the policy of the other compilers, but this will reduce performance (and take more memory).
Reply
#5
Thanks, I just update to s1968 Smile

I thought that if I declare "valor" as uinteger previously, would be enough.

I used to declare the variables according their range of values, but this behavior disconcert me. By now, I will change all the ubyte variables to uinteger, avoiding the problem by this way.
Reply
#6
Haplo Wrote:Thanks, I just update to s1968 Smile

I thought that if I declare "valor" as uinteger previously, would be enough.

I used to declare the variables according their range of values, but this behavior disconcert me. By now, I will change all the ubyte variables to uinteger, avoiding the problem by this way.
The range of valor is ok. The range of the others is not. Ok. This is something we need to discuss. Maybe is better to use the standard behaviour. Basically, in ZXBasic a * b is expanded to Uinteger *after* the multiplication. Other compilers expands first. But this is more expensive...
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)