skip to main content
article
Open Access

The Euclidean definition of the functions div and mod

Published:01 April 1992Publication History
Skip Abstract Section

Abstract

The definitions of the functions div and mod in the computer science literature and in programming languages are either similar to the Algol of Pascal definition (which is shown to be an unfortunate choice) or based on division by truncation (T-definition) or division by flooring as defined by Knuth (F-definition). The differences between various definitions that are in common usage are discussed, and an additional one is proposed, which is based on Euclid's theorem and therefore is called the Euclidean definition (E-definition). Its distinguishing feature is that 0 ≤ D mod d < | d | irrespective of the signs of D and d. It is argued that the E- and F-definitions are superior to all other ones in regularity and useful mathematical properties and hence deserve serious consideration as the standard convention at the applications and language level. It is also shown that these definitions are the most suitable ones for describing number representation systems and the realization of arithmetic operations at the architecture and hardware level.

References

  1. 1 American National Standards Institute. ANSI/MIL-Std 1815 A, Reference Manual for the Ada Programming Language. Alsys, France, Jan. 1983.Google ScholarGoogle Scholar
  2. 2 BOUTE, R. Formal description of digital systems. In Methodolog~es for Computer $ystem Design. W. K. Giloi and B. D. Shriver, Eds., North-Holland, Amsterdam, 1985, pp. 291-306.Google ScholarGoogle Scholar
  3. 3 BOUTE, R. Funmath: Towards a general formalism for system description in engineering applications. In Advances in Electr~cal Engineering Software, P. P. Silvester, Ed., Sprmger~ Verlag, New York, 1990, pp. 215-226.Google ScholarGoogle Scholar
  4. 4 BOUTE, R. On the equivalence of rime-division and frequency-division multiplexing. IEEE Trans. Commun. COM-33, i (Jan. 1985), 97-99.Google ScholarGoogle Scholar
  5. 5 BOUTE, R. Representational and denotational semantics of digital systems. IEEE Trans. Comput. 38, 7 (July 1989), 986-999. Google ScholarGoogle Scholar
  6. 6 BOU~E, R. Syntactic and semantic aspects of formal system description. M~croprocess~ng and Microprogramming 27 (1989), 155-162.Google ScholarGoogle Scholar
  7. 7 CODY, W. J., ET AL. A proposed radix- and word-length-independent standard for fioatingpoint arithmetic. IEEE Micro 4, 4 (Aug. 1984), 86-100.Google ScholarGoogle Scholar
  8. 8 COHEN, D. On holy wars and a plea for peace. IEEE Comput. 14, 10 (Oct. 1981), 48-54.Google ScholarGoogle Scholar
  9. 9 HERSTEIN, I.N. Top~cs in Algebra. Xerox College Publ., Lexington, Mass., 1964.Google ScholarGoogle Scholar
  10. 10 HmFINGER, P.N. Silage Reference Manual, Rev. 1.3. University of California, Berkeley, Dec. 1987.Google ScholarGoogle Scholar
  11. 11 ISO/IEC 7185, Information Technology--Programming Languages--PascaL International Standard, 2nd ed., ISO/IEC 7185, 1990 (E).Google ScholarGoogle Scholar
  12. 12 JAMES, D. V. Multiplexed buses: The endian wars continue," IEEE Micro 10, 3 (June 1990), 9-21. Google ScholarGoogle Scholar
  13. 13 JENSEN, K., AND WmTH, N. Pascal User Manual and Report. 3rd ed., Sprmger-Verlag, New York, 1985. Google ScholarGoogle Scholar
  14. 14 KNUTH, D.E. The Art of Computer Programming, Vol. 1: Fundamental Algor~thms. 2nd ed. Addison-Wesley, Reading, Mass., 1972. Google ScholarGoogle Scholar
  15. 15 McDoNNELL, E. E Complex fioor. In APL Congress 73, P. Gjerl~v et al., Eds., North-Holland, Amsterdam, 1973, pp. 299-305.Google ScholarGoogle Scholar
  16. 16 McDoN~ELL, E. E. Fuzzy residue. In Proceedings of the APL'79 Conference. Rochester, N.Y., June 1979, pp 42-46. Google ScholarGoogle Scholar
  17. 17 STEELE, G. L., JR., Arithmetic shifting considered harmful. SIGPLAN Not. 12, 11 (Nov. 1977), 61-68. Google ScholarGoogle Scholar
  18. 18 STEELE, G. L., JR., Common LISP: The Language. Digital Press, Billerica, Mass., 1984. Google ScholarGoogle Scholar
  19. 19 TURNER, D. Miranda System Manual, Version 1.009. Research Software Ltd., Canterbury, Kent, May 1987.Google ScholarGoogle Scholar
  20. 20 VAN WIJNG^ARDEN, A., ET AL. Revised Report on the Algorithmic Language Algol 68. Springer~Verlag, New York, 1976.Google ScholarGoogle Scholar

Index Terms

  1. The Euclidean definition of the functions div and mod

                Recommendations

                Reviews

                Herbert G. Mayer

                Here is another paper that should have been written decades ago, in which case an existing, widespread incompleteness in programming language design could have been prevented. It points out that in most current programming languages, the elementary arithmetic functions div and mod (for “division” and “remainder”) are defined incorrectly, inconsistently, or not at all for some quadrants. That Euclid wrote the most suitable definition long ago renders this situation in the programming world a bit ludicrous. The paper points out wrong and inconsistent definitions for div and mod functions in several standard languages, discusses options for defining these functions, and compares their respective pros and cons. It also demonstrates that the definition provided by Euclid is most useful for programming, resulting in a strong hint for future machine architects and language designers. The paper justifies the need for the div and mod functions in programming languages and lists a number of numeric attributes these functions should possess. It then presents a range of definitions for div and mod, starting with ISO Pascal. The Pascal definition is followed by a second definition based on division by truncation (T-definition), a third proposed by Knuth and based on division by flooring (F-definition), and finally Euclid's definition (E-definition). Once the Pascal definition is shown to be wrong and incomplete except for the restricted case of solely positive operands, it is dropped from further discussion. The paper discusses division and remainder for negative operands and explains desirable properties. It shows the results in graphic form for integer operands and sketches operand and result ranges for real values. Finally, a table compares and weighs all pros and cons, with the convincing conclusion that the E-definition shows the greatest value in terms of numeric attributes and practicality. During the discussion, the author is objective in that he shows the advantages of options other than the E-definition—the front-runner—and considers practical aspects such as variable-word-length number representations, precision considerations, and implementation on physical architectures. On the surface, the paper is organized poorly; the structure is too complex for a paper less than 20 pages long. There are four levels of structuring, some numeric, some alphabetic, mixing Roman and Arabic, and some parenthesized; the referees should have caught this inconsistency. The editor should have caught the numerous typographical errors. Since the proposed solution is the one defined by Euclid in Alexandria around 300 BC, a literature reference would have been appropriate, but is missing. Occasionally, the author uses terse and mildly obscure mathematical notation that seems to be a subjective preference. To enhance clarity, the author should have offered a table with sample solutions for each definition, each using a consistent range of operands, similar to the way the Ada standard does for the rem and mod functions. The value range should have included an operand pair, for which a desirable property would be violated. The paper does not point out that one of the most widely used languages, C, also specifies the remainder operator % incompletely. These weaknesses are largely cosmetic or not critical. If we accept that div and mod are essential, the paper serves as an essential reference point. Hopefully, future revisions of standard languages and future programming languages will take this paper into account and finally offer solutions to division and remainder-finding in the way the old Greeks did it. Machine architects who argue that one should exploit the gained nanosecond for their favorite version of division can be appeased by providing additional nonstandard versions of division and remainder-finding in programming languages, similar to the PL/I model. That way, cycle-eager machine architects and correctness-conscious programmers can both be satisfied. Boute's paper is the needed catalyst for such a future synthesis. This paper is mandatory reading for anyone amending an existing standard or inventing the next programming language with arithmetic operations. Those who wonder why Ada has both rem and mod operators and why division and remainder-finding for negative operands are undefined in many standard languages will benefit from reading this paper. It is far ahead of virtually all programming language definitions regarding div and mod, yet it is a quarter- century late. The programming community should be grateful for the ideas finally published by Boute in this complete discussion.

                Access critical reviews of Computing literature here

                Become a reviewer for Computing Reviews.

                Comments

                Login options

                Check if you have access through your login credentials or your institution to get full access on this article.

                Sign in

                Full Access

                PDF Format

                View or Download as a PDF file.

                PDF

                eReader

                View online with eReader.

                eReader