SQR

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: SQR
Abbreviation: S, Shift+Q
Type: Function
Token code: 186/$BA
Handling routine
in BASIC ROM:
49009–49015
$BF71–BF77
List of all BASIC keywords


Remark: This article describes the BASIC command SQR in BASIC V2 at the Commodore 64.

Typ: Numeric Function 
General Programming-Syntax: SQR(<Numeric>)

The numeric function SQR is a mathemathical function for square root of a number.

When the term isn't a number, the BASIC error ?TYPE MISMATCH ERROR IN line occurrs. It can be only used number with values from 0 until 1e+38. Then this area is out of range the BASIC error?OVERFLOW ERROR IN line is viewing. By negative values the BASIC error ?ILLEGAL QUANTITY ERROR IN line occurs. When the numeric argument is absent, it appears a ?SYNTAX ERROR IN line.

Accuracy[edit | edit source]

Accuracy of SQR function

The graph to the right was obtained by comparing results from the BASIC SQR function to values computed on a modern x86-type PC.

At least for the plotted domain of [0,+5], SQR matches the true value to 30 bits. No part of the domain proves to be problematic.

SQR calls LOG and EXP, which call POLY1 and POLY2, which call MLTPLY ($BA59), and consequently the multiply bug affects its results. A small number of input values give results that match to only 25 bits.


Examples[edit | edit source]

PRINT SQR(4) Screen: 2
PRINT SQR(15+10) Screen: 5

We show a worked example where we ask the user for a number X. If X is negative, an error message is displayed and control is directed to the end of the block. Otherwise X is positive, then the square root of X is assigned to Y and then shown in a message.

10 INPUT "ENTER NUMBER"; X
20 IF X<0 THEN PRINT "THE NUMBER MUST BE POSITIVE" : GOTO 50
30 Y = SQR(X)
40 PRINT "THE ROOT OF"; X; "IS"; Y
50 END

Two possible tests are:

ENTER NUMBER? 100 
THE ROOT OF 100 IS 10

And:

ENTER NUMBER? -2
THE NUMBER MUST BE POSITIVE



BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT