INT
BASIC keyword | |
Keyword: | INT |
Abbreviation: | – |
Type: | Function |
Token code: | 181/$B5 |
Handling routine in BASIC ROM: |
48332–48360 $BCCC–BCE8 |
List of all BASIC keywords |
Remark: This article describes the BASIC command INT in BASIC V2 at the Commodore 64.
Typ: Integer Function General Programming-Syntax: INT(<Numeric>)
INT is used to round numbers, whereas rounding is different from its common mathematical definition. By positive numbers the fractional part will be cut, while by neagtive numbers the next lower integer value is returned.
By adding 0.5 to the argument the conventional mathemathical rounding can be implemented.
When the argument does not evaluate to a number, the BASIC error ?TYPE MISMATCH ERROR IN line occurs. It can be only used with values from -1e+38 to 1e+38. If the value is out of range, you get the BASIC error?OVERFLOW ERROR IN line. When the numeric argument is absent, it displays a ?SYNTAX ERROR IN line.
Examples[edit | edit source]
PRINT INT(1.53) Screen shows 1 PRINT INT(-9.47) Screen shows -10
For correct rounding add 0.5:
PRINT INT((1.53)+0.5) Screen shows 2 PRINT INT((-9.47)+0.5) Screen shows -9
Positive edge case :
PRINT INT(999999999.5) Screen shows 999999999 PRINT INT(999999999.6) Screen shows 1E+09, although 999999999 is expected
Negative edge case:
PRINT INT(-999999998.9) Screen shows -999999999 PRINT INT(-999999999.0) Screen shows -1E+09
Very big or small numbers in exponential form:
PRINT INT(1.2345678E+20) Value is displayed unchanged as long the comma is aligned
Commercial rounding: π with 3 fractional digits:
PRINT INT((3.14159265*1000)+0.5)/1000 Screen shows 3.142 (rounded up)
FRAC implementation[edit | edit source]
This function declaration for BASIC V2 fills in the missing function FRAC which is similar to INT. It keeps only the fractional part of a float number:
10 DEF FN FRAC(X) = X-SGN(X)*INT(ABS(X)) 20 DEF FN SFRAC(X) = X-INT(X) 30 PRINT π;INT( π);FN FRAC( π);FN SFRAC( π) 40 PRINT -π;INT(-π);FN FRAC(-π);FN SFRAC(-π)
Output:
3.14159265 3 .141592653 .141592653 -3.14159265 -4 -.141592653 .858407347
FN FRAC() is always valid, while in contrasst FN SFRAC() (simple FRAC) is only correct on positive values.
The part following the minus corresponds to the FIX function shown in FIX-Implementierung.
FIX implementation[edit | edit source]
This function declaration for BASIC V2 provides the FIX function (which is included in GW-BASIC Emulator) and simply cuts off the fractional part of a float number:
10 DEF FN FIX(X) = SGN(X)*INT(ABS(X)) 30 PRINT π;INT( π);FN FIX( π) 40 PRINT -π;INT(-π);FN FIX(-π)
Output:
3.14159265 3 3 -3.14159265 -4 -3
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