Forum
Multiplication result greater than 255 in UINTEGER var - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16)
+---- Thread: Multiplication result greater than 255 in UINTEGER var (/showthread.php?tid=846)



Multiplication result greater than 255 in UINTEGER var - LukeBord1 - 05-30-2018

First of all, as usual, sorry if this was discussed before... at a first sight around the arguments, i didn't find a solution.

The below code works till the "32*(PEEK 32044)" multiplicaton returns a a value within 255, but why?...

Code:
REM 32043 and 32044 store line and column values
REM the routine should return the screen attr address
DIM a AS UINTEGER
a = 22528+PEEK 32043+(32*(PEEK 32044))
REM it doesn't work if the result of the multiplication exceeds 255!



Re: Multiplication result greater than 255 in UINTEGER var - LukeBord1 - 05-30-2018

...damn, it isn't the first time i find the solution my own, just after posting the topic here... :lol:

So here's the way to declare the operations:

Code:
DIM a AS UINTEGER
a = 22528+PEEK 32043+(32*(PEEK (UINTEGER, 32044)))

Thanks for watching :oops:


Re: Multiplication result greater than 255 in UINTEGER var - boriel - 05-30-2018

Hi

No worries. Yes it's been discussed before and will be changed in future releases.
ZXbasic does type promoting sometimes but not always. So by default when multiplying two Byte values it will get the result as a Byte hence truncating the result. You can enforce a typecast with CAST of one of the multiplication operands and this should fix it. Try using CAST(Uinteger, 32) and tell me. :wink: