none
modular arithmetic

    Question

  • How do I say "r + c mod 1"

     

    tried

     

    for r as integer = 0 to 3
    
    for c as integer = 0 to 3
    
    x as integer = r + c mod 1
    
    if r + c .isodd then
    
    endif
    
    next
    
    next
    niether works.

     

    Monday, July 26, 2010 7:37 PM

Answers

  • in this instance,, yes. Want to multiply by -1 alternate times.

    If you want to alternate between even/odd then you don't want mod 1, you want mod 2.  Mod 1 will return  the decimal portion of a number.  For any integer input it will always return 0.  For example, 3.14159 mod 1 will return 0.14159.  Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want. 

    thx What was wrong w '%' ?
    • Marked as answer by bob witte Monday, July 26, 2010 9:00 PM
    Monday, July 26, 2010 8:22 PM

All replies

  • % is the operator for modulas, so (r + c) % 1
    • Proposed as answer by Cabe O Monday, July 26, 2010 7:51 PM
    • Unproposed as answer by bob witte Monday, July 26, 2010 9:01 PM
    Monday, July 26, 2010 7:50 PM
  • What would you expect (any number) MOD 1 to return?
    Stephen J Whiteley
    Monday, July 26, 2010 7:51 PM
    Moderator
  • in this instance,, yes. Want to multiply by -1 alternate times.
    Monday, July 26, 2010 7:57 PM
  • expect even mod 1 to return 0 & odd mod 1 to return 1
    Monday, July 26, 2010 8:01 PM
  • exactly; the kind of answer I was hoping for. Alas,                for ro & co integers,

    If (ro + co) % 1 =0 then
                    Else
                    End If

     

    brings up

     

    Error    1    Option Strict On disallows implicit conversions from 'Integer' to 'Boolean'.  

     

    followed by

     

    Error    2    Character is not valid.

    Monday, July 26, 2010 8:05 PM
  • in this instance,, yes. Want to multiply by -1 alternate times.

    If you want to alternate between even/odd then you don't want mod 1, you want mod 2.  Mod 1 will return  the decimal portion of a number.  For any integer input it will always return 0.  For example, 3.14159 mod 1 will return 0.14159.  Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want. 
    Monday, July 26, 2010 8:12 PM
  • in this instance,, yes. Want to multiply by -1 alternate times.

    If you want to alternate between even/odd then you don't want mod 1, you want mod 2.  Mod 1 will return  the decimal portion of a number.  For any integer input it will always return 0.  For example, 3.14159 mod 1 will return 0.14159.  Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want. 

    thx What was wrong w '%' ?
    • Marked as answer by bob witte Monday, July 26, 2010 9:00 PM
    Monday, July 26, 2010 8:22 PM

  • % is used in C#, while Mod is used in VB.Net.

    --
    Mike
    Monday, July 26, 2010 8:38 PM
  • Other than being the modulo operator for C...nothing is intrinsically wrong with %. In BASIC, % is shorthand for Integer types.

    Another quick way to differentiate odd from even is to AND with 1. (Even numbers return 0 and odd ones return 1. There is no division, and you do not need to test for zero to avoid an exception.)

    Monday, July 26, 2010 8:41 PM
  • To altern 0 and 1 this works well

     

        Dim Altern = 1
    
        For x = 1 To 10
          Altern = Altern Xor 1
        Next
    
    Monday, July 26, 2010 11:32 PM
  • Hi Bob Witte,

    MOD gives the remainder.

    So 3 Mod 2 = 1

    4 Mod 2 = 0

     

    Anyway, with one button on a Form try this slight alteration on your code please.

    The output is.>>

    0 + 0 = 0 is even.
    0 + 1 = 1 is odd.
    0 + 2 = 2 is even.
    0 + 3 = 3 is odd.
    1 + 0 = 1 is odd.
    1 + 1 = 2 is even.
    1 + 2 = 3 is odd.
    1 + 3 = 4 is even.
    2 + 0 = 2 is even.
    2 + 1 = 3 is odd.
    2 + 2 = 4 is even.
    2 + 3 = 5 is odd.
    3 + 0 = 3 is odd.
    3 + 1 = 4 is even.
    3 + 2 = 5 is odd.
    3 + 3 = 6 is even.

     

    Option Strict On
    Imports System.Environment
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim sb As New System.Text.StringBuilder
        Dim x As Integer
    
        For r As Integer = 0 To 3
          For c As Integer = 0 To 3
            x = (r + c) Mod 2
            If x = 1 Then
              sb.Append(r.ToString & " + " & c.ToString & " = " & (r + c).ToString & " is odd." & NewLine)
            Else
              sb.Append(r.ToString & " + " & c.ToString & " = " & (r + c).ToString & " is even." & NewLine)
            End If
          Next
        Next
    
        MessageBox.Show(sb.ToString)
    
      End Sub
    End Class
    

     


    Regards, John
    Monday, July 26, 2010 11:58 PM