A function truncating a real number to the lower integer.
This function converts a real number (one with a decimal part) to the nearest integer (whole number) less than the number supplied. Thus,
gives 14, whereas
Alternative Rounding Methods
This type of rounding (or truncation) is known as rounding towards negative infinity and is often implemented by the floor function.
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:
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:
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.