Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VAL incomplete
#1
In ZX SPECTRUM:

LET x = 2
PRINT VAL "2*x"

Prints 4

But on the compiler gives 0

How about adding variables from "a" to "z" in VAL?
Reply
#2
This cannot be done in compiled BASIC. VAL "2*x" will need the ROM VAL, which calls the BASIC interpreter. This will need to store the variables in Sinclair BASIC Format which takes more space and is slow so the ROM interpreter will find the variable "x" in the BASIC Variables region.
Currently, a compiled variable like that of the example takes 1 byte (just the space needed to store the number). For Sinclair BASIC it will always be a Float (5 bytes, and slow) + the variable name "x" encoded in another region, etc.
So VAL here will behave as most BASICs, convert a STRING to a number or return 0 if it cannot decode it (indeed Sinclair BASIC VAL is very powerful).
There is a library function to call the BASIC interpreter, BTW, but it won't work in this case because it still needs the "x" variable.
Reply
#3
(2023-06-26, 08:32 AM)boriel Wrote: VAL "2*x" will need the ROM VAL.



Then, I will do it the hard way:

LET X = 5
LET F$ = "2*X"

LET G$ = VALx$( F$)
( G$ = "2*5" )

PRINT VAL G$
(result = 10)


Code:
' PROGRAM VALx ' (c) ZarSoft 2023 Pascalated Boriel ZX BASIC ' Written by Ze Oliveira #include <input.bas>  ' number = VAL INPUT(12) #include <screen.bas> ' SCREEN$ function #include <attr.bas>  ' ATTR function '--- Pascalated Boriel --- #define PROGRAM  REM #define BEGIN REM '#define CONST    CONST #define VAR      DIM #define INTEGER  LONG #define REAL      FLOAT #define CHAR      STRING '#define STRING    STRING #define BOOLEAN  UBYTE #define TYPE      AS '#define WHILE    WHILE #define REPEAT    DO #define UNTIL    LOOP UNTIL #define PROCEDURE  SUB CONST  TRUE      TYPE BOOLEAN = 1 CONST  FALSE    TYPE BOOLEAN = 0 PROGRAM VALx VAR X TYPE REAL VAR Y TYPE REAL FUNCTION VARaz (V$ TYPE STRING) TYPE REAL VAR R TYPE REAL IF V$ = "Z"   R = Z ELSE IF V$ = "Y"   R = Y ELSE IF V$ = "X"   R = X ENDIF  RETURN R END FUNCTION FUNCTION VALx$ (S$ TYPE STRING) TYPE STRING VAR R$ TYPE STRING R$ = "" FOR i = 0 TO LEN S$ -1   IF S$(i) >= "A"     LET R$ = R$ + STR$ VARaz( S$(i) )   ELSE     LET R$ = R$ + S$(i)   ENDIF  NEXT i RETURN R$ END FUNCTION PROGRAM VALx VAR x,y,z TYPE INTEGER VAR F$ TYPE STRING VAR G$ TYPE STRING VAR sx,sy TYPE REAL VAR sx0,sy0 TYPE REAL VAR first TYPE BOOLEAN LET F$ = "9/(1+8*X*X+8*Y*Y)" FOR y = -3 TO 0   LET first = TRUE   FOR x = -5 TO 5     LET X = x*0.5     LET Y = y*0.5     LET G$ = VALx(F$)     PRINT AT 0,0;"F(x,y)=";G$,,     LET z = VAL(G$)     LET sx = 120 + 20*x - 2.5*y     LET sy = 75 + 6*y + 2.5*x + 10.1*z     IF first       first = FALSE       PLOT sx,sy     ELSE       DRAW sx-sx0,sy-sy0     ENDIF     LET sx0 = sx     LET sy0 = sy    NEXT x NEXT y END PROGRAM
Reply
#4
(2023-06-26, 10:25 AM)zarsoft Wrote: Then, I will do it the hard way:



I have updated
https://forum.boriel.com/showthread.php?tid=2428
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)