Constant Assignment '='

The Constant Assignment operator is used only within CON blocks, to declare compile-time constants. For example,

CON
  _xinfreq = 4096000
  WakeUp   = %00110000 

This code sets the symbol _xinfreq to 4,096,000 and the symbol WakeUp to %00110000. Throughout the rest of the program the compiler will use these numbers in place of their respective symbols. See CON.

These declarations are constant expressions, so many of the normal operators can be used to calculate a final constant value at compile time. For example, it may be clearer to rewrite the above example as follows:

CON
  _xinfreq   = 4096000
  Reset      = %00100000
  Initialize = %00010000
  WakeUp     = Reset | Initialize 

Here, WakeUp is still set to %00110000 at compile time, but it is now more obvious to future readers that the WakeUp symbol contains the binary codes for a Reset and an Initialize sequence for that particular application.

The above examples create 32-bit signed integer constants; however, it is also possible to create 32-bit floating-point constants. To do so, the expression must be expressed 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. Note that if Ratio were defined as 2 / 5 instead of 2.0 / 5.0, the expression would be treated as an integer constant and the result would be an integer constant equal to 0. 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 declaration to convert an integer value to a floating-point value, such as Ratio = FLOAT(2) / 5.0.

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.

For run-time floating-point operations, the FloatMath and FloatString objects provide math functions compatible with single-precision numbers.

See FLOATROUNDTRUNC, 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.