Propeller Manual

Table Of Contents
2: Spin Language Reference – TRUNC
TRUNC
Directive: Remove, “truncate,” the fractional portion from a floating-point constant.
((CON VAR OBJ PUB PRI DAT))
TRUNC ( FloatConstant )
Returns: Integer that is the given floating-point constant value truncated at the decimal point.
FloatConstant is the floating-point constant expression to be truncated to an integer.
Explanation
TRUNC is one of three directives (FLOAT, ROUND and TRUNC) used for floating-point constant
expressions.
TRUNC returns an integer constant that is the given floating-point constant
expression with the fractional portion removed.
Using TRUNC
TRUNC can be used to retrieve the integer portion of a floating-point constant. For example:
CON
OneHalf = 0.5
Bigger = 1.4999
Int1 = trunc(OneHalf)
Int2 = trunc(Bigger)
Int3 = trunc(Bigger * 10.0) + 4
The above code creates two floating-point constants, OneHalf and Bigger, equal to 0.5 and
1.4999, respectively. The next three constants,
Int1, Int2 and Int3, are integer constants that
are based on
OneHalf and Bigger using the TRUNC directive. Int1 = 0, Int2 = 1, and Int3 = 18.
About Floating-Point Constants
The Propeller compiler handles floating-point constants as a single-precision real number as
described by the IEEE-754 standard. Single-precision real numbers are stored in 32 bits, with
a 1-bit sign, an 8-bit exponent, and a 23-bit mantissa (the fractional part). This provides
approximately 7.2 significant decimal digits.
Floating-point constant expressions can be defined and used for many compile-time purposes,
but for run-time floating-point operations, the FloatMath and FloatString objects provide
math functions compatible with single-precision numbers. See the Constant Assignment ‘=’
in the Operators section on page 148,
FLOAT on page 108, and ROUND on page 198, as well the
FloatMath and FloatString objects for more information.
Propeller Manual v1.1 · Page 209