FAQ  •  Register  •  Login

Float variable stores only integer value. Why ?

<<

Eqx

Posts: 11

Joined: Sat Oct 29, 2016 3:41 am

Post Sat Oct 29, 2016 4:08 am

Float variable stores only integer value. Why ?

I'm doing my first tests with ZX Basic. I've tried to create a timer and I found the following issue

  Code:
REM timer test

DIM t1 as float = 0.00

border 0:paper 0:ink 2:cls


while (0=0)
   
    t1 = (65536 * peek 23674 +256 * peek 23673 + peek 23672) /50   
   
    pause int(10+25*RND)
   
    t1 = ((65536 * peek 23674 +256 * peek 23673 + peek 23672) /50) -t1  REM t1 ignores decimal part and has been declared as float

    print at 0,0;t1 REM result is always 0 or 1
   
    if inkey$="s" then
        end   
    end if
     
wend



In example, t1 is declared as float but the variable only stores the integer value, and the final result is wrong. In this case print result is always 0 or 1, ignoring the decimal part
There is something that escapes me or i don't understand?
The same example adapted to Sinclair basic displays and store decimal part correctly.

Thanks in advance
<<

Eqx

Posts: 11

Joined: Sat Oct 29, 2016 3:41 am

Post Tue Nov 08, 2016 10:27 am

Re: Float variable stores only integer value. Why ?

Hola,

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

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Nov 08, 2016 9:40 pm

Re: Float variable stores only integer value. Why ?

Eqx wrote:Hola,

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

Te contesto en inglés por el resto de los lectores del foro. Espero que no te importe... :roll:
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

Can be rewritten as:
  Code:
PRINT PEEK(UInteger, 23672)

And the line
  Code:
return (65536 * peek 23674 +256 * peek 23673 + peek 23672) / cast (fixed,50) 'El cambio efectuado

Can be simplified as:
  Code:
return (PEEK(ULong, 23672) bAND 0xFFFFFF) / cast(fixed, 50)
<<

Eqx

Posts: 11

Joined: Sat Oct 29, 2016 3:41 am

Post Tue Nov 08, 2016 11:16 pm

Re: Float variable stores only integer value. Why ?

Ok boriel, tanks you very much for your explanation
I discovered ZX Basic recently. It's great, congratulations :wink:

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

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

phpBB SEO