Positron Compiler Documentation

Remainder '//'

Positron16 · Revision 1.1.3.5 · PDF page 82

Assignment Variable = Variable // Variable

Return the remainder left after dividing one unsigned or signed value by another.

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

Some division results do not have a whole-number result; they return a whole number and a fraction. For example, 1000 / 6 = 166.667. Integer mathematics does not allow the fractional portion of the result, so 1000 / 6 = 166. However, 166 is an approximate answer, because 166 * 6 = 996. The division operation left a remainder of 4. The // returns the remainder of a given division operation. Numbers that divide evenly, such as 1000/5, produce a remainder of 0: -

  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 = 6
  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX
  MyWord1 = MyWord1 // MyWord2   ' Get remainder of MyWord1 / MyWord2.
  HrsoutLn Dec MyWord1            ' Display the result (4).

' 32-bit modulus
  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 = 99999
  PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX)  ' Make Pin RB14 U1TX
  MyDword = MyDword // MyWord   ' Mod the numbers.
  HrsoutLn Dec MyDword            ' Display the result

The modulus operator does not operate with floating point values or variables. Use fMod for that operation.

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 modulus within a condition. For example:

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