Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

INT

Keyword Abbreviation Token (hex) Version(s) Classification
INT none B5 1.0+ Function

  Syntax  
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
 
  Purpose  
Calculate the "floor" of a number.

 
  Remarks  
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 (notice the numbers are not rounded):
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 rounding (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.
 
  See Also  
/, ABS, AND 

© H2Obsession, 2014
Comments