FLOAT

Directive: Convert an integer constant expression to a compile-time floating-point value.

((CONVAROBJPUBPRIDAT))
   FLOAT (IntegerConstant )


Returns: Resolved value of integer constant expression as a floating-point number.

  • IntegerConstant is the desired integer constant expression to be used as a constant floating-point value.

Explanation

The Propeller does not have a Floating Point Unit (FPU), and there are general better alternatives than relying on floating point for Propeller applications.

FLOAT is one of three directives (FLOAT, ROUND and TRUNC) used for floating-point constant expressions. The FLOAT directive converts a constant integer value to a constant floating-point value.

Using FLOAT

While most constants are 32-bit integer values, the Propeller compiler supports 32-bit floating-point values and constant expressions for compile-time use. Note that this is for constant expressions only, not run-time variable expressions.

For typical floating-point constant declarations, the expression must be shown as a floating-point value in one of three ways: 1) as an integer value followed by a decimal point and at least one digit, 2) as an integer with an E followed by an exponent value, or 3) both 1 and 2. For example:

CON
  OneHalf = 0.5
  Ratio   = 2.0 / 5.0
  Miles   = 10e5

The above code creates three floating-point constants. OneHalf is equal to 0.5, Ratio is equal to 0.4 and Miles is equal to 1,000,000.

Notice that in the above example, every component of every expression is shown as a floating-point value. Now take a look at the following example:

CON
  Two    = 2
  Ratio  = Two / 5.0

Here, Two is defined as an integer constant and Ratio appears to be defined as a floating-point constant. This causes an error on the Ratio line because, for floating-point constant expressions, every value within the expression must be a floating-point value; you cannot mix integer and floating-point values like Ratio = 2 / 5.0.

You can, however, use the FLOAT directive to convert an integer value to a floating-point value, such as in the following:

CON
  Two   = 2
  Ratio = float(Two) / 5.0 

The FLOAT directive in this example converts the integer constant, Two, into the floating-point form of that value so that it can be used in the floating-point expression.

About Floating Point

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, FloatString, Float32, and Float32Full objects provide math functions compatible with single-precision numbers.

See Constant Assignment '=', ROUND, and TRUNC, as well as the FloatMath, FloatString, Float32, and Float32Full objects for more information.

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