Академический Документы
Профессиональный Документы
Культура Документы
Number Conversion
In this
hapter, we dis
uss the issue of
onverting real numbers to integers.
3.1
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
ies 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 dierently. 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
31
November 2, 2007
32
3. Number Conversion
n x n x .
2
In the denition 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 denition 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 dened by
onne
ting it to the
onversion from integers to reals in a
simple equivalen
e. The denition 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 dene 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
November 2, 2007
33
3.2
The denition 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 dierent 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 dierent integers are dierent
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).
Dierent real values do not need to dier 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
[Cancellation]
[x y y+z y+z]
November 2, 2007
34
3. Number Conversion
and
(3.4)
[Cancellation]
Also, for all stri
tly positive real numbers z and all real numbers x and y ,
(3.5)
[Cancellation]
[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.
November 2, 2007
3.3
35
Properties of Floor
The rst time that one en
ounters a denition like denition 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
denition. Two possibilities present themselves immediately; both exploit the fa
t that
the at-most relation is re
exive. The equation
n x n 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 .
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 .)
November 2, 2007
36
3. Number Conversion
Note that it is not permissible to instantiate n with some real value x . The dening
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 dening equation: in general, we have
p q p q .
This is the rule of
ontraposition . So the
ontrapositive of the denition 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 .
Here we al ulate:
November 2, 2007
{
x y
37
x x y
=
x x
xy .
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 denition of the
oor fun
tion, denition 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 denition 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.
November 2, 2007
38
3. Number Conversion
n
jp
{
n is an integer, denition 3.1
p
n x
arithmeti }
n2 x n < 0
=
n2 x n < 0
=
n x
{
n is an integer, denition 3.1
x .
n
jp
x .
jp k
x is an integer, by denition, we
an now
jp k
x . We
an also instantiate n to
instantiate n to
x , for the same reason. In
jp k
That is,
jp
jp
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 denition (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":
November 2, 2007
39
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 exemplied 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
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
denition of
oor }
arithmeti
}
denition of
oor }
m/n x
=
arithmeti }
m n x .
November 2, 2007
40
3. Number Conversion
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 denition is a dual
of the denition of the
oor fun
tion.
Definition 3.15
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
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).
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
lmm
n
The rst step in the
al
ulation is perhaps the most di
ult. This is be
ause the denition of the
eiling fun
tion, denition 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 dening 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
m
k1 <
n
arithmeti
, assuming 0 < n
{
n(k1) < m
=
integer inequalities }
n(k1) + 1 m
=
{
k
=
k
m+n1
n
{
denition
of
oor fun
tion: (3.1) }
m+n1
n
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
denition of eiling }
m
k
n
{
inequalities }
m
< k+1
n
{
ontrapositive of denition of
oor
jmk
< k+1
n
{
inequalities }
jmk
k .
n
lmm
n
2
Exercise 3.20
f su h that f.(x) = x .
November 2, 2007
3.6. Summary
43
mn =
jmk
n
Suppose the denition 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 denition in Java amounts to a
ase analysis on whether the sign of m is equal
to the sign of n . If so, the rst denition 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 denition 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
denition 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.
November 2, 2007