3 Replies Latest reply on Oct 3, 2018 10:36 AM by dan_liddell

    Modulo operator

    Wahbi

      Hello,

       

      I would like to use modulo operator in the SVRF device property computations. Example: x = 5 mod 2 = 1.

      Any idea?

       

      Thanks,

      W

        • 1. Re: Modulo operator
          chris_balcom

          The % character used for modulo in many areas of SVRF doesn't seem to be supported for builtin languages within SVRF such as device property computation. I'm not aware of a workaround.

          • 2. Re: Modulo operator
            dan_liddell
            Modulo is based upon the Euclidean division algorithm, which is this. For integers a, d,  q, and r:
            a = qd + r
            here q is the quotient, d is the divisor, and r is the remainder. Note that d is not 0.
            Based upon the Euclidean definition of division, the remainder must always be non-negative, and this can be guaranteed. Here are some examples:
            7 =  2(3)  + 1
            7 = -2(-3) + 1
            -7 = -3(3)  + 2
            -7 =  3(-3) + 2
            If we isolate the remainder, we have  this:
            r = a - qd
            But this is not how Donald Knuth decided to define % in C. He defined it this way:
            r = a - (floor(a/d))d
            or just commuting the final 2 terms
            r = a - d(floor(a/d))
            where floor( ) means truncation toward the negative direction. Using our cases above, we have this:
            r =  7 - 3(floor(7/3))     =  7 - 3(2)     =  1
            r =  7 - (-3(floor(7/-3))  =  7 - (-3(-3)) = -2
            r = -7 - 3(floor(-7/3))    = -7 - 3(-3)    =  2
            r = -7 - (-3(floor(-7/-3)) = -7 - (-3(2))  = -1
            This definition can be more useful in computational situations than the number-theoretical definition. Note that the sign of the remainder is the same as the sign of the divisor in this scheme.
            Assuming that you want the C implementation (%) and not the number-theoretical version, you might be able to implement the following in the device property computation. In the built-in languages, we have the  trunc( ) function, which truncates toward 0 (instead of toward the negative  direction).
            # a is your dividend, d is your divisor, r is your remainder
            # the program will need to guarantee d !=  0
            if (a/d) >= 0
            r = a - d*(trunc(a/d))
            else
            # trunc rounds toward 0. for a/d < 0, this means we need  to subtract 1 from trunc(a/d)
            r = a - d*(trunc(a/d)-1)
            using our examples above:
            r =  7 -   3(trunc(7/3))     =  7 - 3(2)                      = 1
            r =  7 - (-3(trunc(7/-3)-1)) =  7 - (-3(-2-1)) = 7 - (-3(-3)) = -2
            r = -7 -   3(trunc(-7/3)-1)) = -7 - 3(-2-1))   = -7 - 3(-3)   = 2
            r = -7 - (-3(trunc(-7/-3))   = -7 - (-3(2))                   = -1
            So that might be a worthwhile solution.
            1 of 1 people found this helpful
            • 3. Re: Modulo operator
              venkateshwarlu

              Thank you. I was looking for the same solution. "r = a - d*(trunc(a/d))" is working instead of the modulus operator