1 Number Coversio Rolad Backhouse March 5, 2001
Outlie 2 We discuss the mathematical properties of covertig real umbers to itegers. I particular, we show how to implemet roudig up (of ratioal umbers) i terms of roudig dow. This provides a mechaism for implemetig roudig up i Java (which defies iteger divisio as roudig towards zero). The discussio illustrates the use of equivaleces i defiig importat mathematical otios.
Castig 3 Castig is the ame give i laguages like C ad Java for the operatio of covertig a value of oe type to aother. The cast from real umbers to itegers occurs whe evaluatig a iteger divisio. The text 1/2 (for example) meas a iteger divisio ad evaluates to zero. So, if the real value is iteded, oe has to write, say, 1.0/2.0. Addig decimal poits does t help, however, whe the values beig divided are expressios. So if, for example, m ad have bee declared to be itegers the we have to write somethig like (real)m/(real) to force the compiler to compute the real value of m/.
The Floor Fuctio 4 Defiitio: For all real x, x is a iteger such that, for all itegers, x x.
The Floor Fuctio 5 Defiitio: For all real x, x is a iteger such that, for all itegers, x x. O the right side of the equivalece, the at-most relatio ( ) is betwee reals whereas o the left side it is betwee itegers. Makig all coversios explicit, temporarily adoptig a Java-like otatio, is illumiatig. Doig so the defiitio becomes that, for all real x, (floor)x is a iteger such that for all itegers, (floor)x (real) x. So the floor of x is defied by coectig it to the coversio from itegers to reals i a simple equivalece.
Properties of Floor 6 Defiitio: For all real x, x is a iteger such that, for all itegers, x x. NB. x ca be istatiated to a iteger, but caot be istatiated to a real.
Properties of Floor 7 Defiitio: For all real x, x is a iteger such that, for all itegers, x x. NB. x ca be istatiated to a iteger, but caot be istatiated to a real. 1. x is by defiitio a iteger. So we ca istatiate to x. We get x x x x. Simplifyig, x x.
Properties of Floor 8 Defiitio: For all real x, x is a iteger such that, for all itegers, x x. NB. x ca be istatiated to a iteger, but caot be istatiated to a real. 1. x is by defiitio a iteger. So we ca istatiate to x. We get x x x x. Simplifyig, x x. 2. Istatiate x to.we get. Simplifyig,.
Properties of Floor 9 Defiitio: For all real x, x is a iteger such that, for all itegers, x x. NB. x ca be istatiated to a iteger, but caot be istatiated to a real. 1. x is by defiitio a iteger. So we ca istatiate to x. We get x x x x. Simplifyig, x x. 2. Istatiate x to.we get. Simplifyig,.
3. Istatiate x i 1. to. We get 10. 4. Combiig 2. ad 3., for all itegers =.
Properties of Floor Summary 11 For all reals x, For all itegers, x x. =.
Properties of Floor (Cotiued) 12 Recall the rule of cotrapositio: p q p q. The cotrapositive of the defiitio of the floor fuctio is, for all itegers ad real x, But ( m) m <. So ( x ) ( x). x < x <.
Properties of Floor (Cotiued) 13 Recall the rule of cotrapositio: p q p q. The cotrapositive of the defiitio of the floor fuctio is, for all itegers ad real x, But ( m) m <. So ( x ) ( x). x < x <. Istatiatig with x +1 ad simplifyig we deduce: x < x +1. Recallig that x x, we have established x x < x +1.
Idirect Equality 14 More complex properties use the rule of idirect equality: x = y z : z x z y. Example: x +m = { arithmetic } m x = { defiitio of floor } m x = { arithmetic } x+m = { defiitio of floor } Hece, x+m. x +m = x+m.
Roudig Off 15 Ceilig Fuctio Defiitio: For all real x, x is a iteger such that, for all itegers, x x. Exercise: derive properties of the ceilig fuctio dual to the properties of the floor fuctio.
The Problem 16 Roudig dow a iteger divisio of positive umbers m ad is expressed by m where m is the real divisio of m ad. Dually, roudig up is expressed by. Implemetig roudig up give a implemetatio of roudig dow amouts to fidig suitable values p ad q so that p =. q The values p ad q should be expressed as arithmetic fuctios of m ad (that is, fuctios ivolvig additio ad multiplicatio, but ot ivolvig the floor or ceilig fuctios).
The Strategy 17 We ca derive suitable expressios for p ad q usig the rule of idirect equality. For arbitrary iteger k, we aim to elimiate the ceilig fuctio from the iequality k obtaiig a iequality of the form k e where e is a arithmetic expressio i m ad. We may the coclude that e =.
The Solutio k 18
The Solutio k 19 = { iteger arithmetic } k 1 < = { cotrapositive of defiitio of ceilig } k 1 < m
The Solutio k 20 = { iteger arithmetic } k 1 < = { cotrapositive of defiitio of ceilig } k 1 < m = { arithmetic, > 0 } (k 1) < m = { iteger iequalities } (k 1) + 1 m = { arithmetic, > 0 } k m+ 1
The Solutio k 21 = { iteger arithmetic } k 1 < = { cotrapositive of defiitio of ceilig } k 1 < m = { arithmetic, > 0 } (k 1) < m = { iteger iequalities } (k 1) + 1 m = { arithmetic, > 0 } k m+ 1 = { defiitio of floor fuctio } m+ 1 k.
We have derived: k k m+ 1 Here k is arbitrary. So, by idirect equality, we get m+ 1 =. I Java, therefore, if it is required to roud up the result of dividig m by oe should compute (m+-1)/.. 22