Introduction
A function truncating a real number to the lower integer.
X=INT(Y)
Example | Result |
---|---|
INT(99.8) | 99 |
INT(-12) | -12 |
INT(-12.1) | -13 |
This function converts a real number (one with a decimal part) to the nearest integer (whole number) less than the number supplied. Thus,
INT(14.56)
gives 14, whereas
INT(-14.5)
gives -15.
Alternative Rounding Methods
This type of rounding (or truncation) is known as rounding towards negative infinity and is often implemented by the floor function.
DEF FN_floor(n)=INT(n)
Rounding in the opposite direction, towards positive infinity, is often implemented by the ceiling function and can be implemented in BBC BASIC using the following function:
DEF FN_ceiling(n)=-INT(-n)
Another common type of truncation is rounding towards zero. This has the effect of removing the fractional part of the number and leaving the whole part intact, so +3.14 and -6.28 would end up as 3 and -6 respectively. A BBC BASIC function implementation could be implemented as follows:
DEF FN_truncate(n)=SGN(n)*INT(ABS(n))
You may also wish to round to the nearest whole integer.
DEF FN_round(n) LOCAL m : m=ABS(n) : IF m-INT(m)<0.5 =SGN(n)*INT(m) ELSE =SGN(n)*-INT(-m)
FN_round(2.3) returns 2, FN_round(5.8) returns 6, FN_round(-12.1) returns -12 and FN_round(-3.6) returns -4.
Syntax
<n-var>=INT<numeric>