ABS

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: ABS
Abbreviation: A Shift+B
Type: Function
Token code: 182/$B6
Handling routine
in BASIC ROM:
48216–48218
$BC58–BC5A
List of all BASIC keywords


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

Type: Numeric Function 
General Programming-Syntax: ABS(<numeric-expression>)

ABS evaluates to the absolute value (value without the sign) of the given numeric term.

The numeric parameter has to be a floating point number. If the term is not numeric, the BASIC error ?TYPE MISMATCH ERROR occurs.
When the numeric argument is absent, it displays a ?SYNTAX ERROR.

Examples[edit | edit source]

Parameters[edit | edit source]

PRINT ABS(-10) (Screen: 10)
PRINT ABS(-10-11+5) (Screen: 16)
PRINT ABS("Text") (Screen: ?TYPE MISMATCH  ERROR)
PRINT ABS() (Screen: ?SYNTAX  ERROR)

Re-implementing the SGN function[edit | edit source]

The ABS function is helpful to re-implement the SGN function:

100 DEF FN SG(X)=X/(ABS(X)-(X=0))
200 FOR I=-10 TO 10 STEP 10
210 PRINT I,SGN(I),FN SG(I)
220 NEXT

The first column shows the value to be tested, the second column the built-in SGN function value and the third column the value of the recreated function.
Following output will be shown

-10       -1        -1
 0         0         0
 10        1         1

The new defined function FN SG(X) covers also the special case for value 0 by using the additional term -(X=0) (gives the value 1) to avoid a division by zero condition.

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