The Mod Function |
Written by Mike James | |||||||
Friday, 04 December 2020 | |||||||
Page 3 of 3
Joe Celko Comments:The MOD() FunctionThe 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
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:
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
3) The addition property is preserved where "⊕" is modular addition defined by
4) Subtraction is preserve under modular subtraction, which is defined as
From this definition, we would get the following:
Related ArticlesTo 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.
Comments
or email your comment to: comments@i-programmer.info
<ASIN:1598220020> <ASIN:0750687096> <ASIN:0071371923>
|
|||||||
Last Updated ( Friday, 04 December 2020 ) |