11-08-2016, 09:40 PM
Eqx Wrote:Hola,Te contesto en inglés por el resto de los lectores del foro. Espero que no te importe... :roll:
He podido solucionar ésto usando CAST. Entiendo que ZXBasic si no se le indica lo contrario explícitamente, hace la operaciones por defecto con enteros (es una suposición)
Ahora si que funciona.
Code:Dim t as fixed
t = timer () + 0.25
print t; ' Ahora si opera con decimales
function timer ()
return (65536 * peek 23674 +256 * peek 23673 + peek 23672) / cast (fixed,50) 'El cambio efectuado
end function
Gracias !
Un saludo
This is exactly the same question asked here.
ZXBasic will delay type promotion as much as possible unless the programmer specifies otherwise (using CAST).
In your case, PEEK is by default UByte. 65536 is Long, so Peek value is converted first to Long (32 bits) and then multiplied. This is expensive in the Z80 since it is a 8 bit architecture.
Fixed is even more expensive, but cheaper than Float (40 bits). Your solution is better, because all the operations are done with 32 bits and then converted to Fixed.
Converting them to Fixed first will lead to a larger and slower program.
BTW, ZXBasic PEEK allows to peek larger values than Ubyte. You can PEEK a Uinteger, ULong or any other value.
So:
Code:
PRINT PEEK(23672) + 256 * PEEK(23673): REM PEEKing an Uinteger at 23672
Code:
PRINT PEEK(UInteger, 23672)
Code:
return (65536 * peek 23674 +256 * peek 23673 + peek 23672) / cast (fixed,50) 'El cambio efectuado
Code:
return (PEEK(ULong, 23672) bAND 0xFFFFFF) / cast(fixed, 50)