The Mod Function
Written by Mike James   
Friday, 04 December 2020
Article Index
The Mod Function
Int
Some More Advanced Ideas

Joe Celko Comments:

The MOD() Function 

The modulo or remainder function is actually trickier than it looks. It appears in most programming languages of have numeric datatypes. If m is zero, then we get a division by zero exception. Otherwise, the result is the unique non-negative exact numeric value r with scale zero such that 

  1.  r has the same sign as n.
  2.  the absolute value of r is less than the absolute value of m.
  3.  n = m * k + r for some exact numeric value k with scale zero.

This is tricky when the values of n and m are not cardinals (i.e., positive, non-zero integers).

Experiment and find out how your Favorite programming language handles negative numbers and decimal places.

This was a major issue for the Pascal Standard at one time, versions of FORTRAN also had different implementations. Originally, MOD(n, m) was defined only for positive values of both m and n, and leaves the result to be implementation-dependent when either of m or n is negative.

Negative values of n have no required mathematical meaning and that many implementations of MOD either do not define it at all, or give some result that is the easiest to calculate on a given hardware platform.

However, negative values for (m) do have a very nice mathematical interpretation that we wanted to see preserved in the SQL definition of MOD(). Len Gallagher of NIST proposed the following rules to use in the SQL standard, along with the usual SQL convention That if either parameter is a NULL, then the result is a NULL:

  1. If n is positive, then the result is the unique non_negative exact numeric quantity r with scale zero such that r is less than m and n = (m * k) + r for some exact numeric quantity k with scale zero .
  2. Otherwise, the result is an implementation-defined exact numeric quantity r with scale zero which satisfies the requirements that r is strictly between m and (-m), and that n = (m * k) + r for some exact numeric quantity k with scale zero, and a completion condition is raised: warning -- implementation-defined result.

This definition guarantees that the MOD() function, for a given positive value of n, will be a homomorphism under addition from the mathematical group of all integers, under integer addition, to the modular group of integers {0, 1..., m-1} under modular addition. This mapping then preserves the following group properties:

1)  The additive identity is preserved: MOD(0, m) = 0

2)  Additive inverse is preserved in the modular group defined by

MOD(-MOD(n, m), m) = m - MOD(n, m)

MOD(-n, m) = - MOD(n, m)

3) The addition property is preserved where "⊕" is modular addition defined by MOD((MOD(m, m) + MOD(n, m)), m)

MOD((m + n), m) = MOD(m, m) ⊕ MOD(n, m)

4) Subtraction is preserve under modular subtraction, which is defined as MOD((MOD(m, m) ⊖ MOD(n, m)), m)

MOD(m-n, m) = MOD(m, m) ⊖ MOD(n, m)

From this definition, we would get the following:

MOD(12, 5) = 2
MOD(-12, 5) = 3

Public Key Encryption  

XOR - The Magic Swap

Introduction to Boolean Logic

JavaScript Bit Manipulation       

Dates are difficult

      

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 
Banner


How Memory Works

Exactly how does computer memory work? What is surprising is that  it still works in more or less the same way as when Babbage designed his Analytical Engine or the IBM 360 accessed core memory.  [ ... ]



Binary Arithmetic

Do you know binary? There are only 10 possible answers and even if it's a 1 it's still fun to consider the wider concepts.


Other Articles
 

 

<ASIN:1598220020>

<ASIN:0750687096>

<ASIN:0071371923>

 



Last Updated ( Friday, 04 December 2020 )