The Euclidean definition of the functions div and mod - CORE

12 downloads 0 Views 1MB Size Report
integer division, interpolation, mod function, number representation, ... INTRODUCTION ... to wrong or erroneous definitions, as in the 1S0 Standard for Pascal [11, 13], .... (restricted to integers. D and d, d # O). (i) For div: if I D I < \ d 1,then. D div d = O; otherwise ... [11] for the range of ..... the set A. We also use the notational.
The Euclidean and mod RAYMOND University

Definition of the Functions

div

T. BOUTE of Nijmegen

The definitions of the functions div and mod in the computer science literature and in programming languages are either similar to the Algol or 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 O s D mod d < I d I 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.

and Logic Structures]: Miscellaneous Categories and Subject Descriptors: B.2.m [Arithmetic —arithmetic circuits, formal description; D. 3.0 [Programming Languages]: General —standards; D.3.3 [Programming Languages]: Language Constructs—data Wes and structures; G.2. 1 [Discrete Mathematics]: G. 1.0 [Numerical Analysis]: General–computer arithmetic; Combinatorics; J.2 [Computer engineering, General

mathematics

Terms: Design,

Applications]:

Physical Sciences and Engineering–e~ectronzcs,

and statistics Standardization,

Theory

Additional Key Words and Phrases: Decimation, div function, Euclid’s theorem, hardware description, integer division, interpolation, mod function, number representation, predefine functions, remainder, residue, sampling

1. INTRODUCTION The

viewpoint

originally

presented

developed

here

regarding

in the context

of formal

the

div

and

description

mod

functions

of computer

was

architec-

paper is motivated by subsequent experience with the ture [51. The present role of these functions in various areas of applied mathematics and engineering. Indeed, the functions div and mod are very important concepts in discrete

Author’s

mathematics

current

address:

for

certain

Department

problems

of Computer

in

number

Science,

theory,

University

in

computer

of Nijmegen,

Toer-

nooiveld 1, NL 6525 ED Nijmegen, The Netherlands. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 01992 ACM 0164-0925/92/0400-0127 $01.50 ACM Transactions on Programming Languages and Systems, Vol 14, No. 2, April 1992, Pages 127-144

128

.

science tions

Raymond

T. Boute

for reasoning

engineering

multiplexing,

about

number

for a variety

representation

of issues

ranging

systems, from

in communica-

coding

to sampling

and

and so on.

Hence it is unfortunate handled rather casually

that the definition of these functions appears to be in the computer science literature and in the design

of programming

languages,

tional engineering” the 1S0 Standard

decisions down to wrong or erroneous definitions, as in for Pascal [11, 13], Algol 68 [201, and some other languages.

In this

paper

as one might

we clarify

the

infer

differences

from

between

particular those based on division by truncation by flooring (F-definition) as defined by Knuth

various

poor

the various

“defini-

definitions,

in

(T-definition) and on division [141. We also propose still

another definition, which we call Euclidean because it is based on Euclid’s theorem (E-definition). This alternative is rarely discussed in the literature, yet on closer analysis it is advantageous in terms of regularity and useful mathematical Euclidean

properties, definition

over a wide

variety

languages.

theoretically emerged

of representative

the need for a div-mod Comparison F-definitions

both

usually function

of the various as the standard Secondary

and

as the application

definitions convention

in ALU

design

definitions

Note.

that

problem

can

For positive

is clearly with

values

usage.

areas where

The

choice,

we experienced

leads to a preference for the E- and in mathematics and programming always

uniform way [5]. Although this suggests realizations than for the other definitions, study

practical straightforward

pair.

be expressed

primary ones to cater for the exceptional cases. At the computer architecture level, the preferred most important number representation and computer

ties such as the are also avoided.

in

most

definitions arithmetic

of the

capture systems

the in a

the possibility of simpler hardware verifying this requires a thorough

beyond

arithmetic

of dividend

in terms

the scope of this shifting

and divisor,

pointed

paper.

Difficul-

out by Steele

all definitions

[17]

of div and

mod agree, as expected. Clearly, any reasons for preferring one convention over another must be derived from cases in which the definitions differ, viz., when either dividend or divisor or both are negative.

2. OVERVIEW 2.1

OF DEFINITIONS

Conventions

Unless stated numbers that

and Classification otherwise, D (the dividend) and d (the divisor) stand for real are arbitrary except for the fact that d # O. There are several

reasons for using integers. (1) The generality

the

more

general

comes at no extra

setting

of real

cost whatsoever

numbers

rather

in terms

than

of notation

just

or

conceptual understanding; it is perhaps even beneficial in this respect. The specialization of the discussion to integers is immediate, viz., by restricting D and d to values in z without changing any of the formulas ACM Transactions

on Programming

Languages and Systems, Vol. 14, No. 2, April

1992.

The Euclidean Definition of the Functions div and mod For integer values, (a < b) @ (a s b – 1),and in this I D mod d I < I d I maybe replaced by the equivalent –l if desired.

presented. inequality dl < Idl (2) In

certain

periodic

application function

(3) Unnecessary

requiring

As we shall

the

period

restrictions

overlooked,

tions

areas,

f with

in

generality

revision

number

is needed,

may

d-values

cause

and

naturally should

case the I D mod

example, issues

to be

stage. in certain

not

a

p).

certain

at later

arise

representation)

for

f x = f( x mod

of the definition

see, also negative

(including

generality

p satisfies

129

.

applica-

be ignored.

There-

fore, with few exceptions such as ISO Standard Pascal [11], most programming languages define both D div d and D mod d for negative d-values as well.

Hence

we can consider

D, respectively:

four

qO (+ + ), ql

“quadrants”

according

quadrants appear as a clarification in later figures. tion to complex numbers, for instance, in the manner [151, falls occasion.

outside

Presently, following

the

ds Z

that

(integer

(b) D = d “ (D div I D mod

in

of d and

the

paper

and

functions

can be considered

div

of these

The further generalizaproposed by McDonnell

and mod

at another

at least

satisfy

the

conditions:

(a) D div

(c)

scope of this

we require

to the signs

(+ – ), q2 (– + ), q3 (– – ). The names

d) + D mod

d I < Id I

addition

to

d = D/d.

quotient

must d

(division

( I remainder

the

implicit

Definitions

not

be integer),

basic

rule),

I < I divisor condition

satisfying

1),

that,

these

if

conditions

D / d G Z, are

then

D

considered

div

wrong

because they lack the properties one expects from arithmetic and, in fact, have no useful mathematical properties at all. Condition (b) is the most important

one in this

respect.

Note

that,

because

of (b), the

two

functions

must be considered as paired, that is, their definitions are not independent. The above discussion leads to three classes of definitions: (1) wrong

definitions;

(2) div-dominant

definitions;

(3) mod-dominant Here

definitions.

“ ~dominant”

the function division rule pair. 2.2

Wrong

(1)

that

the

definition

of or a suitable

definition, of the other

restriction

which together function of the

on

with the div-mod

Definitions

1S0

Standard

means

f constitutes the primary (b) yields the definition

Standard

Pascal

[11] is as follows

(i) For div: if

Definition. (restricted

I D I < \ d 1, then \D\-ldl

together

with

sign(D

div

D div

The to integers

D and

d = O; otherwise

(). For instance, if D = –3 and d = 7, then D div d = () and to k = – 1).Hence rule.

D mod d = 4 (corresponding which violates the division (2)

Variant

defines

(i) For div: (ii)

Pascal

the following divide

The

Definition. variant

ISO

(restricted

and truncate

(D div

Pascal

User

to integers

(for a precise

+ D mod

d)d

Manual

[13,

p. 17]

d, d # O):

D and

definition,

d = 4,

see Section

2.3).

For mod: let rem = D – (D div d) . d; ifrem0

for D mod

d. However,

an additional (but most likely not deliberate) error d it contains violation of condition (c). For instance, if D = – 3 and d = – 7,

then

Standard

violates the division rule if rem

Suggest Documents