Вы находитесь на странице: 1из 13

Chapter 3

Number Conversion
In this hapter, we dis uss the issue of onverting real numbers to integers.

3.1

The Floor Function

Casting is the name given in languages like C and Java for the operation of onverting

a value of one type to another. Casts often o ur automati ally, but the programmer
should still be aware of when they o ur be ause they an ause errors if not used
properly.
The ast from real numbers to integers o urs when evaluating an integer division.
The real value of the division is rst omputed and then the value is onverted to an
integer, for example by rounding towards zero. The ast from integers to reals may
seem unne essary |one might argue that an integer is a real value anyway| but it is
required in languages like Java, and indeed sometimes needs to be made expli it. This
is be ause the division operator \/" is overloaded in Java. The text 1/2 (for example)
means an integer division and evaluates to zero. So, if the real value is intended, one
has to write, say, 1.0/2.0. Adding de imal points doesn't help, however, when the
values being divided are expressions. So if, for example, m and n have been de lared to
be integers, we have to write something like (real)m/(real)n to for e the ompiler to
ompute the real value of m/n . (We say \something like" here be ause we don't want
to go into all the intri a ies of a spe i language. In fa t, there is no \real" type in
Java; there are two integer types and two oating point types.)
Most languages spe ify how onversion from reals to integers is done. Java, for example, spe i es that it is to be done by rounding towards zero. But, there are ir umstan es
when a programmer wishes the ast to be evaluated di erently. For example, when ompleting a tax return, the tax payer is allowed to round in a dire tion in their own favour.
Thus, interest re eived would be rounded down whilst interest paid would be rounded
up. The values entered may also be omputed, for example by dividing interest between

Mathemati s for Computing S ien e Roland Ba khouse.

31

November 2, 2007

32

3. Number Conversion

several partners a ording to pre-negotiated ratios. The programmer of a tax- al ulator


must therefore fully understand the properties of the asting operators. This is the topi
of this hapter.
In mathemati s, there is no spe i notation for asting an integer value to a real
value. Mathemati ians all it an \embedding" and usually rely on a notational onvention, like using m and n to denote integers and x and y to denote reals, in order to
make lear what is intended. When spe i referen e to the embedding is needed, it
is usual to introdu e some lo al ad-ho notation. There is, however, a well-established
mathemati al notation for onverting reals to integers. Mathemati ians have identi ed
two su h fun tions, the oor fun tion and the eiling fun tion. For real value x , the
oor of x is an integer and is denoted x . Also, for real value x , the eiling of x is
an integer and is denoted x . To begin with, we onsider only the oor fun tion; we
return to the eiling fun tion later.
The oor fun tion from reals to integers is de ned as follows: for all real x , we
de ne x (read \ oor x ") to be the greatest integer that is at most x . Formally, this
is aptured by a simple equivalen e.
Definition 3.1 (Floor Function)
integers n ,

For all real x , x is an integer su h that, for all

n x n x .
2

In the de nition of the oor fun tion we use the mathemati al onvention of not
denoting the onversion from integers to reals. It is impli it in the inequality n x ,
whi h seems to ompare an integer with a real. In fa t, what is meant is the omparison
of the real value orresponding to n with the real value x . On the right side of the
equivalen e the at-most relation (\ ") is between reals whereas on the left side it is
between integers.
Making expli it both onversions, temporarily adopting a Java-like notation, is illuminating. Doing so the de nition be omes that, for all real x , (floor)x is an integer
su h that for all integers n ,
n (floor)x (real)n x .

So, the oor of x is de ned by onne ting it to the onversion from integers to reals in a
simple equivalen e. The de nition of the oor fun tion is an instan e of what is alled a
Galois onne tion. In general, a Galois onne tion relates (or onne ts) two fun tions
by a simple equivalen e of the same shape as that above; Galois onne tions are used
to de ne a ompli ated fun tion (like the oor fun tion) by mapping its properties into
the properties of a simpler fun tion (like the embedding of integers into the reals). This

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.2. The Signi an e of Types

33

said, it is useful to adopt the mathemati al onvention of omitting expli it mention of


the embedding fun tion and this is what we do from now on.

3.2

The Significance of Types

The de nition of the oor fun tion uses a onvention that is ommon in mathemati al
texts. The onvention is that variables like x and y at the end of the alphabet denote
real values, and variables like m and n in the middle of the alphabet denote integers.
This makes it easy to identify the so- alled type of a variable.
It is very important to know what the type of a variable is be ause properties of
overloaded operators and relations depend on the types. A good illustration is given by
the at-most and less-than relations on real and on integer numbers.
The symbols \ " and \ < " are used whether or not the values being ompared are
integers or reals, but the relations have di erent properties depending on their domain!
This is very important to appre iate, and will be parti ularly important shortly when
we dis uss the oor fun tion.
For all integers m and n , we have the property:
(3.2)

[One-step]

[m n m < n+1]

This is a fundamental property. It expresses the fa t that di erent integers are di erent
by at least one. Instan es of the rule are 2 2 2 < 2+1 (both sides are true) and
3 2 3 < 2+1 (both sides are false).
Di erent real values do not need to di er by at least one. So there is no su h property
for real numbers. That is,
x y x < y+1

is not true for all real values x and y . Indeed, the instan e x , y := 0.5 , 0 is false: the
left side of
0.5 0 0.5 < 0+1

is false and its right side is true.


Note that (3.2) is a property of the at-most and less-than relations on the integers.
Mistakes that an o ur when using these relations are aused by the overloading of the
operator symbols.
Another example of where overloading an ause errors is the use of the same symbol
\ / " for real and integer division. Important properties of addition and multipli ation
(on real or integer values) is that they are an ellable with respe t to both less-than
and at-most. For any real values x , y and z ,
(3.3)

[Cancellation]

[x y y+z y+z]

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

34

3. Number Conversion

and
(3.4)

[Cancellation]

[x < y y+z < y+z]

Also, for all stri tly positive real numbers z and all real numbers x and y ,
(3.5)

[Cancellation]

[x < y xz < yz]

[Cancellation]

[x y xz yz]

and
(3.6)

Division by a real number z is just the same as multipli ation by the real number 1/z .
So (3.6) is equivalent to the property: for all stri tly positive real numbers z and all
real numbers x and y ,
(3.7)

[Cancellation]

[x y x/z x/z]

However, integer division does not enjoy the same properties. Unlike in Java or other
ASCII-based programming languages, let us not overload the \ / " symbol; instead, let
us write mn for the integer division of integer m by integer n . Then integer division
is monotoni with respe t to the at-most relation: for all stri tly positive integers p
and all integers m , n
(3.8)

[Monotonicity]

[m n mp np]

(Note the impli ation rather than equality between the two sides.) But it is not monotoni with respe t to the less-than relation. Monotoni ity would mean that
m < n mp < np

for all stri tly positive integers p and all m and n . But, for example,
0 < 1 02 < 12

has the value false (the left side is true and the right side is false, so the impli ation is
false).
Errors an (and do) o ur in programming languages be ause operator symbols (like
\/" and \<") are overloaded. It is important to be aware of the types of the variables in
order to avoid the errors. With this warning, let us now return to the dis ussion of the
oor fun tion.

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.3. Properties of Floor

3.3

35

Properties of Floor

The rst time that one en ounters a de nition like de nition 3.1, it an be di ult to
see how it is used. But, it is not as di ult as it may seem.
The rst thing we an do is to try to identify some spe ial ases that simplify the
de nition. Two possibilities present themselves immediately; both exploit the fa t that
the at-most relation is re exive. The equation
n x n x

is true for all integers n and reals x . Also, x is by de nition an integer. So we an


instantiate n to x . We get
x x x x .

The left-hand side that is obtained | x x | is true, and so the right-hand side is
also true. That is,
x x .

This tells us that the oor fun tion rounds down. It returns an integer that is at most
the given real value. (Note that this is not the same as rounding towards zero. For
negative numbers, rounding down rounds away from zero. So the Java real-to-integer
onversion oin ides with the oor fun tion only for positive values.)
The se ond possibility is to instantiate x to n . This is allowed be ause every integer
is a real. Stri tly, however, we are instantiating x to the real value obtained by onverting
n . We get
n n n n .

In this ase, it is the right side of the equivalen e that is true. So we an simplify to
n n .

Earlier, we determined that x x for all real values x . Instantiating x to n , we get


n n .

Combining the two inequalities, we have derived that, for all integers n ,
(3.9)

n = n .

(Formally, the property of the at-most relation we use is that it is antisymmetri . That
is, for all numbers m and n , m = n exa tly when both m n and n m .)

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

36

3. Number Conversion

Note that it is not permissible to instantiate n with some real value x . The de ning
equation is true for all integers n , but a real value is not an integer.
A good understanding of the equivalen e operator suggests something else we an do
with the de ning equation: in general, we have
p q p q .

This is the rule of ontraposition . So the ontrapositive of the de nition of the oor
fun tion is, for all integers n and real x ,
(n x) (n x) .

But, (n m) m < n . So
x < n x < n .

Equally, using that for integers m and n , m < n m+1 n ,


x +1 n x < n .

Now we an exploit re exivity of the at-most relation again. Instantiating n with x +1


and simplifying we dedu e:
x < x +1 .

Re alling that x x , we have established


x x < x +1 .

In words, x is su h that x is at most x and x is less than x +1 . Be ause x is


an integer, this de nes it uniquely. We an express the uni ity by a simple equivalen e:
for all integers m and all reals x ,
(3.10) m = x m x < m+1 .
Re alling the dis ussion of integer division, we now ask whether the oor fun tion is
monotoni . That is, we want to show that
x y x y .

Here we al ulate:

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.4. Indire t Equality


x y
=

{
x y

de nition 3.1, x,n := y, x


transitivity of

37

x x y
=

x x

xy .

Thus, the oor fun tion is, indeed, monotoni .

3.4

Indirect Equality

Let us now demonstrate how to derive more ompli ated properties of the oor fun tion.
In the pro ess we introdu e an important te hnique for reasoning with Galois onne tions
alled the rule of indire t equality .
The following property illustrates the te hnique.
(3.11)

jp

jp

 
x

 
x .

 
x

for all x , 0 x .
Suppose we want to establish this property. It is an equality between two oor values;
yet the de nition of the oor fun tion, de nition 3.1, seems to suggest that we should
prove it by proving the two inequalities

and

jp

This strategy turns out to be rather di ult to arry out. A better strategy is this: the
form of the left side of de nition 3.1 is an inequality between an arbitrary integer and a
oor value. So let's begin with the expression
n

jp

where n is arbitrary, and see what we an dis over. This is how the al ulation goes.

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

38

3. Number Conversion
n

jp

{
n is an integer, de nition 3.1
p
n x

arithmeti }

n2 x n < 0
=

n2 is an integer, de nition 3.1

n2 x n < 0
=

arithmeti , assuming that 0 n

n x

{
n is an integer, de nition 3.1
 
x .
n

So we have proved that, for all integers n , where 0 n ,


n

jp

 
x .

jp k
x is an integer, by de nition, we an now
jp k
 
x . We an also instantiate n to
instantiate n to
x , for the same reason. In

Sin e n is an arbitrary integer, and


the former ase, we get
jp

jp k

That is,

jp

jp

 
x .

 
x .

Similarly, in the latter ase, we get:


jp k
 
x .
x

Property (3.11) now follows from the antisymmetry of the at-most relation.
Note that the de ision on how to prove the theorem, i.e. the introdu tion of the
integer n , is entirely inspired by the shape of de nition (3.1). The only way we an
al ulate something about the oor fun tion is to use its spe i ation. That spe i ation
allows one to rewrite the oor fun tion only when it is in some spe ial shape. In this
ase: it is on the greater side of the at-most relation and on the smaller side there is
an (arbitrary) integer. That the spe i ation of the oor fun tion is indeed a good basis
for al ulating properties of the fun tion is due to the rule of \indire t equality":

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.4. Indire t Equality

39

Rule of indire t equality

Two numbers l and m are equal if it is the ase that, for all numbers n of the
same type as l and m ,
nl nm .

The quali ation on n in the rule means that (for example) if l and m are reals, the
property n l n m must be true of all real numbers, n ; if, however, l and m are
known to be, say, natural numbers, the property must be true of all natural numbers, n ,
and, if l and m are, say, even integers, the property must be true of all even integers,
n.
Prove the rule of indire t equality making lear how the properties of
equivalen e and the at-most ordering are used. Where does the requirement that all of
l , m and n have the same type enter into the proof?
Exercise 3.12

Using the same al ulational style exempli ed above, prove the following properties of the oor fun tion :
Exercise 3.13

(a) x+m = x +m ,
(b) x/m = x /m (assuming m is a positive integer).
2
Exercise 3.14

What is wrong with the following \proof"?

We have, for integers m and n , where n is stri tly positive, and all real x ,
m n x
=

{
m nx

{
m/n x

de nition of oor }
arithmeti }
de nition of oor }

m/n x
=

arithmeti }

m n x .

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

40

3. Number Conversion

Thus, by indire t equality, n x = n x for all positive n . (Hint: he k every


step arefully to see that all requirements have been met.)
Give examples of n and x for whi h n x 6= n x . (Hint: nd values m , n
and x that demonstrate the error in the above proof.)
2

3.5

Rounding Off

To round o this hapter, we show how the programmer an take ontrol of rounding in
integer division.
Re all the dis ussion at the beginning of the hapter where we said that in a tax
al ulation it is sometimes ne essary to round down and sometimes to round up. We want
to show how to implement rounding up integer divisions supposing that our programming
language always rounds down.
In order to express the problem we need the eiling fun tion. The de nition is a dual
of the de nition of the oor fun tion.
Definition 3.15

For all real x , x is an integer su h that, for all integers n ,

x n x n .
2

We leave it as an exer ise to the reader to derive properties of the eiling fun tion
dual to the properties of the oor fun tion derived in se tion 3.3.
Rounding down an integer division of positive numbers m and n is expressed by
jmk

n
m
where
is the real division of m and n . Dually, rounding up is expressed by
n
lmm
.
n

Implementing rounding up given an implementation of rounding down amounts to nding


suitable values p and q so that
  l m
m
p
=
.
q
n

The values p and q should be expressed as arithmeti fun tions of m and n (that
is, fun tions involving addition and multipli ation, but not involving the oor or eiling
fun tions).

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.5. Rounding O

41

We an al ulate suitable expressions for p and q using the rule of indire t equality.
Spe i ally, for arbitrary integer k , we aim to eliminate the eiling fun tion from the
inequality
k

lmm
n

obtaining an inequality of the form


ke

where e is an arithmeti expression in m and n. We may then on lude that


e =

lmm
n

The rst step in the al ulation is perhaps the most di ult. This is be ause the de nition of the eiling fun tion, de nition 3.15, provides a rule for dealing with inequalities
where a eiling value is on the lower side of an at-most relation but not when it is on the
higher side (whi h is the ase we are interested in). However, re alling our dis ussion of
the oor fun tion, the solution is to onsider the ontrapositive of the de ning equation.
Spe i ally we have, by negating both sides of 3.15,
(3.16) n < x n < x .
We an now pro eed with the derivation:
k
=

lmm
n

k1 <
=

integer arithmeti }

lmm
n

ontrapositive of de nition of eiling (rule (3.16)) }

m
k1 <
n
arithmeti , assuming 0 < n
{

n(k1) < m
=

integer inequalities }

n(k1) + 1 m
=

arithmeti , assuming 0 < n

{
k

=
k

m+n1
n
{

de nition
of oor fun tion: (3.1) }


m+n1
n

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

42

3. Number Conversion

Here k is arbitrary. So, by indire t equality, we get, for all m and n , where 0 < n ,
(3.17)

lmm
n

m+n1
=
n

In Java, therefore, if it is required to round up the result of dividing integer m by stri tly
positive integer n , one should ompute (m+n-1)/n . (If n is negative then, of ourse,
both m and n should be negated before applying the formula.)
Note how the assumption 0 < n emerged naturally during the above
al ulation; multipli ation by n is only monotoni with respe t to the less-than relation
when n is stri tly positive. Show that the assumption is ne essary by using (3.17) to
evalute
Exercise 3.18

1
1

What do you get?


2
Exercise 3.19
lmm
n

What is wrong with the following derivation?


k

de nition of eiling }

m
k
n
{
inequalities }
m
< k+1
n
{
ontrapositive of de nition of oor
jmk
< k+1
n
{
inequalities }
jmk
k .
n

Thus, by indire t equality,


jmk
n

lmm
n

2
Exercise 3.20

Constru t a fun tion

f su h that f.(x) = x .

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

3.6. Summary

43

Integer division an be de ned independently of real division. A


possible de nition is: for integers m and n (where n 6= 0 ), mn is the largest integer
k su h that kn m .
Rephrase this de nition in the form of an equivalen e onne ting division to multipli ation. Use your de nition to establish
Exercise 3.21

mn =

jmk
n

Suppose the de nition is: for integers m and n (where n 6= 0 ), mn is the smallest
integer k su h that kn m . What is the equivalen e in this ase?
(The de nition in Java amounts to a ase analysis on whether the sign of m is equal
to the sign of n . If so, the rst de nition above is taken, if not the se ond.)
2

3.6

Summary

In this hapter we have seen how the onversion of real numbers to integer numbers is
given a mathemati al de nition by onne ting it to the opposite onversion, from integer
numbers to real numbers. Spe i ally, we have studied the oor and eiling fun tions
and their mathemati al properties. The on luding example showed how to onstru t a
de nition of rounding up an integer division in terms of rounding down.

3.7

Bibliographic Remarks

A good sour e for further dis ussion of the oor and eiling fun tions is the book \Con rete Mathemati s" [? by Graham, Knuth and Patashnik.

Mathemati s for Computing S ien e Roland Ba khouse.

November 2, 2007

Вам также может понравиться