Positron Compiler Documentation

Divide '/'

Positron16 · Revision 1.1.3.5 · PDF page 81

Assignment Variable = Variable / Variable

Divides variables and/or constants, returning an unsigned or signed 8, 16, 32-bit or floating point result.

Variables may be constants, variables or expressions. Assignment Variable can be any valid variable type.

The Divide operator works exactly as you would expect with signed or unsigned integers from - 2147483648 to +2147483647 as well as floating point.

  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 16      ' Tell the compiler the device will be operating at 16MHz

  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyWord1 as Word = 1000
  Dim MyWord2 as Word = 5
  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyWord1 = MyWord1 / MyWord2   ' Divide the numbers.
  HrsoutLn Dec MyWord1            ' Display the result (200).

' 32-bit division
  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 16      ' Tell the compiler the device will be operating at 16MHz

  Declare Hserial1_Baud = 9600
  Declare Hrsout1_Pin = PORTB.14

  Dim MyWord as Word = 100
  Dim MyDword as Dword = 10000

  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX

  MyDword = MyDword / MyWord      ' Divide the numbers.
  HrsoutLn Dec MyDword            ' Display the result

The PIC24® and dsPIC33® range of devices have an exception mechanism built into their hardware. One of these mechanisms is a trap of a division by zero on any of its instructions. The compiler attempts to avoid this state, however, it cannot guarantee that an exception will not occur, and because it is a compiled language, it cannot always give an error message for such an event.

If the BASIC code seems to reset at a particular place within the code, make sure it is not a division by 0. If a suspected division by zero will occur, wrap the division within a condition. For example:

If MyVar2 <> 0 Then
  DestVar = MyVar1 / MyVar2
EndIf
View original PDF page 81Open page in PDFOriginal PDF page 81