Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mixing variable types in a numerical calculation
#2
This is a recurrent question:

You're facing truncation because y * 32 is computed on first place (* has precedence over +), and y * 32 = 576 ( > 255). Some compilers do type promotion (coercion) automatically. ZX Basic does not because for Z80 (8 bits, no multiplication instruction) this will degrade performance. In the future this feature will be added as an option.

You can cast explicitly:
Code:
attrMem=22528+CAST(Uinteger, y)*32+x
The above will fix it, signaling that y value must used as an Uinteger in that multiplication. See CAST for an explanation.

Anyway, there's already a very fast function for calculating the ATTR address:
Code:
#include <attr.bas>

DIM x,y AS UBYTE
CLS
y=18
x=4
POKE AttrAddr(y, x),16 'red paper, black ink

ATTR function is implemented in a library (#include <attr.bas>). And so is POINT.
Reply


Messages In This Thread
RE: Mixing variable types in a numerical calculation - by boriel - 01-05-2021, 12:13 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)