Positron Compiler Documentation

32-bit and 64-bit FloatIng Point Maths

Source: Positron16 Compiler User Manual, PDF page 32

The Positron16 compiler performs single precision (32-bit) IEEE754 FloatIng Point calculations and double precision (64-bit) IEEE754 FloatIng Point calculations.

Declaring a variable as Float or Double will enable 32-bit or 64-bit floating point calculations on that variable.

Dim MyFloat as Float
Dim MyDouble as Double

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 FlNum = 5.0 ' Create a floating point value of a whole number

Note.

It is important to remember that floating point arithmetic is not the ultimate in accuracy, it is merely a means of compressing a complex or large value into a small space (4 bytes in the case of 32-bit Floats, and 8 bytes in the case of 64-bit Doubles), in essence, it is an approximation of a value. 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 and 64-bit floating point math is quite microcontroller intensive since the microcontroller is only a 16-bit processor. It also consumes quite large amounts of RAM, and code space for its operation, therefore always use floating point sparingly, and only when strictly necessary.

Unlike Positron8 for 8-bit microcontrollers, which uses a modified floating point format, Positron16 uses the IEEE754 standard.

An IEEE754 single precision float variable has three components: a sign bit telling whether the number is positive or negative, an exponent giving its order of magnitude, and a mantissa specifying the actual digits of the number. Below is the bit layout:

For 32-bit Float:

 seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm  meaning
31                              0  bit number

For 64-bit Double:

 seeeeeeeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmm....  meaning
63                                          0  bit number

s = sign bit, e = exponent, m = mantissa

32-bit FloatIng Point Example Programs.

' Multiply two floating point values
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Dim MyFloat as Float
  Symbol cFlNum = 1.234
                                     ' Create a floating point constant value
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  MyFloat = cFlNum * 10
  HrsoutLn Dec MyFloat
' Add two floating point variables
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Dim MyFloat as Float
  Dim MyFloat1 as Float
  Dim MyFloat2 as Float
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  MyFloat1 = 1.23
   MyFloat2 = 1000.1
  MyFloat = MyFloat1 + MyFloat2
   HrsoutLn Dec MyFloat
' A digital volt meter, using the on-board 10-bit ADC
   Device = 24FJ64GA002
                                     ' Select the device to compile for
   Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
   Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Declare Adin_Tad = cFRC
                                   ' RC OSC chosen
  Declare Adin_Delay = 10
                                   ' Allow 10us sample time
  Dim wRaw as Word
  Dim fVolts as Float
  Symbol cQuanta = 3.3 / 1024
                                     ' Calculate the quantising value for 10-bits
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  AD1CON2 = 0
                                     ' +Vref = AVdd, -Vref = AVss
  AD1PCFGbits_PCFG0 = 0
                                     ' Analogue input on AN0
   Do
     wRaw = ADin 0
     fVolts = wRaw * cQuanta
     HrsoutLn Dec2 fVolts, "V"
     DelayMs 300
   Loop

64-bit FloatIng Point Example Programs.

' Multiply two 64-bit floating point values
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600     ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14  ' Select the pin for TX with USART1
  Dim MyDouble as Double
  Symbol cFlNum = 1.234
                                     ' Create a floating point constant value
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  MyDouble = cFlNum * 10
  HrsoutLn Dec MyDouble
' Add two 64-bit floating point variables
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600     ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14  ' Select the pin for TX with USART1
  Dim MyDouble as Double
  Dim MyDouble 1 as Double
  Dim MyDouble 2 as Double
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  MyDouble1 = 1.23
  MyDouble2 = 1000.1
  MyDouble = MyDouble1 + MyDouble2
  HrsoutLn Dec MyDouble
' A digital volt meter, using the on-board 10-bit ADC
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Declare Adin_Tad = cFRC
                                     ' RC OSC chosen
  Declare Adin_Delay = 10
                                     ' Allow 10us sample time
  Dim wRaw as Word
  Dim fVolts as Double
  Symbol cQuanta = 3.3 / 1024
                                     ' Calculate the quantising value for 10-bits
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  AD1CON2 = 0
                                     ' +Vref = AVdd, -Vref = AVss
  AD1PCFGbits_PCFG0 = 0
                                     ' Analogue input on AN0
  Do
     wRaw = ADin 0
     fVolts = wRaw * cQuanta
     HrsoutLn Dec2 fVolts, "V"
     DelayMs 300
  Loop

Notes.

Any expression that contains a floating point variable or constant will always be calculated as a floating point, even if the expression also contains integer constants or variables. The same applies for Doubles. If an expression has a mix of 32-bit floats and 64-bit doubles, the it will be carried out as 64-bit Double.

If the assignment variable is an integer variable, but the expression is of a floating point nature, then the floating point result will be converted into an integer.

  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
                         ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Dim MyDword as Dword
  Dim MyFloat as Float
  Symbol cPI = 3.14
  RPOR7 = 3
                                     ' Make PPS Pin RP14 U1TX
  MyFloat = 10
'
' Float calculation with result 13.14, reduced to integer 13
'
  MyDword = MyFloat + cPI
  HrsoutLn Dec MyDword
                                     ' Display the integer result 13

Subtopics