The Mod Function
Written by Mike James
Friday, 04 December 2020
Article Index
The Mod Function
Int

## 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) = 2MOD(-12, 5) = 3`

#### Related Articles

Storage Mapping Functions

Public Key Encryption

XOR - The Magic Swap

Introduction to Boolean Logic

JavaScript Bit Manipulation

Dates are difficult Introduction To The Genetic AlgorithmGenetic algorithms pop up all over computer science and applied computing. They are simple, easy to apply and easy to understand. What mystery remains is why they work at all? How can something seemin [ ... ] + Full Article The Computer - What's The Big Idea?There can't be a more central concept than “the computer” to what programmers know, but it is such a commonplace that we tend to take it for granted and miss how truly amazing it is. It might just [ ... ] + Full Article Other Articles

<ASIN:1598220020>

<ASIN:0750687096>

<ASIN:0071371923>

Last Updated ( Friday, 04 December 2020 )