FAQ  •  Register  •  Login

bug or missunderstood?

<<

Haplo

User avatar

Posts: 9

Joined: Tue May 12, 2015 5:09 pm

Post Thu Oct 27, 2016 4:35 pm

bug or missunderstood?

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?
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Mon Nov 07, 2016 9:50 am

Re: bug or missunderstood?

:shock:
This definitely looks like a bug.
What compiler version are you using??
<<

Haplo

User avatar

Posts: 9

Joined: Tue May 12, 2015 5:09 pm

Post Mon Nov 07, 2016 12:06 pm

Re: bug or missunderstood?

Hello, the version is 1.4.0-s1967
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Mon Nov 07, 2016 10:32 pm

Re: bug or missunderstood?

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).
<<

Haplo

User avatar

Posts: 9

Joined: Tue May 12, 2015 5:09 pm

Post Tue Nov 08, 2016 5:48 pm

Re: bug or missunderstood?

Thanks, I just update to s1968 :)

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.
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Nov 08, 2016 8:41 pm

Re: bug or missunderstood?

Haplo wrote:Thanks, I just update to s1968 :)

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...

Return to Bug Reports

Who is online

Users browsing this forum: No registered users and 2 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO