ROUND

Directive: Round a floating-point constant to the nearest integer.

((CONVAROBJPUBPRIDAT))
   ROUND ( FloatConstant )


Returns: Nearest integer to original floating-point constant value.

  • FloatConstant is the floating-point constant expression to be rounded to the nearest integer.

Explanation

ROUND is one of three directives (FLOAT, ROUND and TRUNC) used for floating-point constant expressions. ROUND returns an integer constant that is the closest integer value to the given floating-point constant expression. Fractional values of ½ (.5) or higher are rounded up to the nearest whole number while lower fractions are rounded down.

Using ROUND

ROUND can be used to round floating-point constants up or down to the nearest integer value. Note that this is for compile-time constant expressions only, not run-time variable expressions. For example:

CON
  OneHalf   = 0.5
  Smaller   = 0.4999
  Rnd1      = round(OneHalf)
  Rnd2      = round(Smaller)
  Rnd3      = round(Smaller * 10.0) + 4 

The above code creates two floating-point constants, OneHalf and Smaller, equal to 0.5 and 0.4999, respectively. The next three constants, Rnd1, Rnd2 and Rnd3, are integer constants that are based on OneHalf and Smaller using the ROUND directive. Rnd1 = 1, Rnd2 = 0, and Rnd3 = 9.

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 '=', FLOAT, and TRUNC, as well as the FloatMath and FloatString objects for more information.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.