Home > CBM > BASIC > Keywords > Int 180 Subpages: (divide), (equal), (less), (minus), (more), (multiply), (plus), (power), Abs, And, Append, Asc, Atn, Auto, Backup, Bank, Begin, Bend, Bload, Boot, Box, Bsave, Bump, Catalog, Char, Chr, Circle, Close, Clr, Cmd, Collect, Collision, Color, Concat, Cont, Copy, Cos, Data, Dclear, Dclose, Dec, Def, Delete, Dim, Directory, Dispose, Dload, Do, Dopen, Draw, Ds, Ds string, Dsave, Dverify, El, Else, End, Envelope, Er, Err, Exit, Exp, Fast, Fetch, Filter, Fn, For, Fre, Get, Get num, Getkey, Go, Gosub, Goto, Graphic, Gshape, Header, Help, Hex, If, Input, Input num, Instr, Joy, Key, Left, Len, Let, List, Load, Locate, Log, Loop, Mid, Monitor, Movspr, New, Next, Not, Off, On, Open, Or, Paint, Peek, Pen, Pi, Play, Pointer, Poke, Pos, Pot, Print, Print num, Pudef, Quit, Rclr, Rdot, Read, Record, Rem, Rename, Renumber, Restore, Resume, Return, Rgr, Right, Rlum, Rnd, Rreg, Rspcolor, Rsppos, Rsprite, Run, Rwindow, Save, Scale, Scnclr, Scratch, Sgn, Sin, Sleep, Slow, Sound, Spc, Sprcolor, Sprdef, Sprite, Sprsav, Sqr, Sshape, St, Stash, Step, Stop, Str, Swap, Sys, Tab, Tan, Tempo, Then, Ti, Ti string, To, Trap, Troff, Tron, Until, Using, Usr, Val, Verify, Vol, Wait, While, Width, Window, Xor |
| Keyword | Abbreviation | Token (hex) | Version(s) | Classification |
| INT | none | B5 | 1.0+ | Function |
INT ( value )
| Parameters | Type | Legal Value(s) | Default Value | Note(s) |
| value | Numeric | any | | |
| Returns | Type | Value(s) | Note(s) |
| result | Integer* | any | Not assignable to an integer variable unless -32768 to 32767 |
Calculate the "floor" of a number.
The INT function returns the largest integer less than or equal to the supplied value.
For positive numbers, this is like chopping off any fractional part (it is never rounded up).
For negative numbers that are not already integers, the next lower integer is returned.
Hopefully the examples below will clarify, but you might also like to think of this as
"round towards negative infinity."
* Like most BASIC math functions and operators, the INT function actually returns the result
as a floating-point "type" of number (in terms of BASIC numeric types). However it is gauranteed
to be a mathematical integer; that is, with no digits (or all zeros) after the decimal point.
BASIC limits several things to be a certain kind of integer, but this function is generally NOT useful
with them. Where BASIC limits things to integers, it will automatically convert a floating-point value
to an integer automatically (just as if this function were used).
Because this function is called automatically in those cases, INT is not needed.
The reason I mention this is because the result may not be acceptable in some cases
where an integer is required. For example, to store a value in an integer-type variable,
the result must be -32768 to +32767; but there is no gaurantee the result will be
in that range. Similarly, some BASIC statements and functions want an unsigned integer (0 to 65535)
or a byte (0 to 255).
INT can be used to round numbers with a little trick (see examples).
It is also useful to calculate the remainder after division (so called modulus).
You will get TYPE MISMATCH ERROR with a string value.
Example with simple positives and negitves (round down):
PRINT INT(2.0)
2
READY.
PRINT INT(2.1)
2
READY.
PRINT INT(2.9)
2
READY.
PRINT INT(-2.0)
-2
READY.
PRINT INT(-2.1)
-3
READY.
PRINT INT(-2.9)
-3
READY.
|
Examples of round-to-nearest (just add 0.5):
PRINT INT(2.0 + 0.5)
2
READY.
PRINT INT(2.1 + 0.5)
2
READY.
PRINT INT(2.9 + 0.5)
3
READY.
PRINT INT(-2.0 + 0.5)
-2
READY.
PRINT INT(-2.1 + 0.5)
-2
READY.
PRINT INT(-2.9 + 0.5)
-3
READY.
|
Examples of remainder (modulus):
PRINT 10-3*INT(10/3) : REM 10 mod 3, or remainder of 10/3
1
READY.
PRINT 69-16*INT(69/16) : REM 69 mod 16 or remainder of 69/16
5
READY.
|
(divide), ABS, SGN
© H2Obsession, 2014 |