Integer variables can be used in assignments (using keywords INPUT, LET, READ, FOR, ENDPROC and PROC) by preceding their name with a % symbol.
Normally, it is not possible to access standard numeric variables or functions within an integer expression, or to access integer variables or operations within a standard numeric expression. In the following program:
10 a=3
20 b=4
30 %a=2
40 %b=5
50 c=%a*b
60 d=%b * a
70 PRINT c,d
80 %b=b
90 c=%a*b
100 PRINT c,d
you might expect line 70 to produce 8 and 15. Instead it returns 10 and 10 as the % in lines 50 and 60 indicates that the entire expression is an integer expression, and all the variables named in each line, are integer variables even though each name is not directly preceded by a % and only line 100 produces a different output; 8 and 10 respectively.
It is, as apparent from the above example, possible therefore, to assign an integer expression to a standard normal numeric variable, or vice-versa, and the value will be converted appropriately. This automatic conversion is called casting and it's best illustrated in line 80 above as well as the examples below which are all valid assignments:
%A=2*PI*radius
assigns a truncated floating point calculation to integer variable A
%B=%B+(A(7)<<3)
shifts integer array element A(7) left 3 bits and adds it to integer variable B
addr=%x(1)<<8+x(0)
calculates standard numeric variable addr from low and high bytes in integer array X elements 0 and 1.
As we saw earlier it's not normally possible to use a floating point expression within an integer expression. But what if we needed to do so? Consider the following example:
%a,%b,%c=1:PRINT %a+PI+b+c
Looks simple enough, doesn't it? All we expect to happen is for casting to take over and use just the integer portion of the value of PI, but it doesn't work that way. Instead the cursor flashes next to PI and the NextBASIC editor complains. To address this, NextBASIC includes the special INT {fp_expression} keyword (do not omit the braces) which converts (casts) any floating point expression fp_expression into an integer. So even if the example above wouldn't work, a small change:
%a,%b,%c=1:PRINT %a+INT { PI }+b+c
and it works happily! As a matter of fact INT {...} will convert any expression that produces a floating point value. Here are some examples:
test = 3.45: PRINT % INT {test}
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.