Hola,
Floating Point Math
The PROTON+ compiler can perform 32 x 32 bit IEEE 754 "Compliant" Floating Point calculations.
Declaring a variable as FLOAT will enable floating point calculations on that variable.
DIM FLT AS FLOAT
To create a floating point constant, add a decimal point. Especially if the value is a whole number.
SYMBOL PI = 3.14 " Create an obvious floating point constant
SYMBOL FL_NUM = 5.0 " Create a floating point format value of a whole number
Floating point arithmetic is not the utmost in accuracy, it is merely a means of compressing a complex or large value into a small space (4 bytes in the compiler"s case). Perfectly adequate results can usually be obtained from correct scaling of integer variables, with an increase in speed and a saving of RAM and code space. 32 bit floating point math is extremely microcontroller intensive since the PICmicro is only an 8 bit processor. It also consumes large amounts of RAM, and code space for its operation, therefore always use floating point sparingly, and only when strictly necessary. Floating point is not available on 12-bit core PICmicros because of memory restrictions, and is most efficient when used with 16-bit core devices because of the more linear code and RAM specifications.
Floating Point Format
The PROTON+ compiler uses the Microchip variation of IEEE 754 floating point format. The differences to standard IEEE 745 are minor, and well documented in Microchip application note AN575 (downloadable from
www.microchip.com).
Floating point numbers are represented in a modified IEEE-754 format. This format allows the floating-point routines to take advantage of the PICmicro"s architecture and reduce the amount of overhead required in the calculations. The representation is shown below compared to the IEEE-754 format: where s is the sign bit, y is the LSb of the exponent and x is a placeholder for the mantissa and exponent bits.
The two formats may be easily converted from one to the other by manipulation of the Exponent and Mantissa 0 bytes. The following assembly code shows an example of this operation.
Format Exponent Mantissa 0 Mantissa 1 Mantissa 2
IEEE-754 sxxx xxxx yxxx xxxx xxxx xxxx xxxx xxxx
Microchip xxxx xxxy sxxx xxxx xxxx xxxx xxxx xxxx
IEEE-754 TO MICROCHIP
RLF MANTISSA0
RLF EXPONENT
RRF MANTISSA0
MICROCHIP TO IEEE-754
RLF MANTISSA0
RRF EXPONENT
RRF MANTISSA0
Variables Used by the Floating Point Libraries
Several 8-bit RAM registers are used by the math routines to hold the operands for and results of floating point operations. Since there may be two operands required for a floating point operation (such as multiplication or division), there are two sets of exponent and mantissa registers reserved (A and B). For argument A, PBP_AARGHHH holds the exponent and PBP_AARGHH, PBP_AARGH and PBP_AARG hold the mantissa. For argument B, PBP_BARGHHH holds the exponent and PBP_BARGHH, PBP_BARGH and PBP_BARG hold the mantissa.
Floating Point Example Programs
" Multiply two floating point values
DEVICE = 18F452
XTAL = 4
DIM FLT AS FLOAT
SYMBOL FL_NUM = 1.234 " Create a floating point constant value
CLS
FLT = FL_NUM *10
PRINT DEC FLT
STOP
" Add two floating point variables
DEVICE = 18F452
XTAL = 4
DIM FLT AS FLOAT
DIM FLT1 AS FLOAT
DIM FLT2 AS FLOAT
CLS
FLT1 = 1.23
FLT2 = 1000.1
FLT = FLT1 + FLT2
PRINT DEC FLT
STOP
" A digital voltmeter, using the on-board ADC
DEVICE = 16F877
XTAL = 4
ADIN_RES = 10 " 10-bit result required
ADIN_TAD = FRC " RC OSC chosen
ADIN_DELAY = 50 " Allow 50us sample time
DIM RAW AS WORD
DIM VOLTS AS FLOAT
SYMBOL QUANTA = 5.0 / 1024 " Calculate the quantising value
CLS
TRISA = %00000001 " Configure AN0 (PORTA.0) as an input
ADCON1 = %10000000 " Set analogue input on PORTA.0
AGAIN:
RAW = ADIN 0
VOLTS = RAW * QUANTA
PRINT AT 1,1,DEC2 VOLTS,"V "
GOTO AGAIN
Notes
Floating point expressions containing more than 3 operands are not allowed, due to the extra RAM space required for a software stack. Any expression that contains a floating point variable or value will be calculated as a floating point. Even if the expression also contains a byte, word, or dword value or variable.
If the assignment variable is a byte, word or dword variable, but the expression is of a floating point nature. Then the floating point result will be converted into an integer.
DEVICE = 16F877
DIM DWD AS DWORD
DIM FLT AS FLOAT
SYMBOL PI = 3.14
FLT = 10
DWD = FLT + PI " Float calculation will result in 13.14, but will be reduced to integer 13
PRINT DEC DWD " Display the integer result 13
STOP
For a more in-depth explanation of floating point, download the Microchip application notes AN575, and AN660. These can be found at
www.microchip.com.
Code space requirements
As mentioned above, floating point accuracy comes at a price of speed, and code space. Both these issues are not a problem if a 16-bit core device is used, however 14-bit core devices can pose a problem. The compiler attempts to load the floating point libraries into low memory, along with all the other library subroutines, but if it does not fit within the first 2048 bytes of code space, and the PICmicro has more than 2048 bytes of code available, the floating point libraries will be loaded into the top 1000 bytes of code memory. This is invisible to the user, however, the compiler will warn that this is occurring in case that part of memory is being used by your BASIC program.
More Accurate Display or Conversion of Floating Point values
By default, the compiler uses a relatively small routine for converting floating point values to decimal, ready for RSOUT, PRINT STR$ etc. However, because of its size, it does not perform any rounding of the value first, and is only capable of converting relatively small values. i.e. approx 6 digits of accuracy. In order to produce a more accurate result, the compiler needs to use a larger routine. This is implemented by using a DECLARE: -
FLOAT_DISPLAY_TYPE = LARGE or STANDARD
Using the LARGE model for the above declare will trigger the compiler into using the more accurate floating point to decimal routine. Note that even though the routine is larger than the standard converter, it actually operates much faster. The compiler defaults to STANDARD if the declare is not issued in the BASIC program.