Exact Computer Calculations With Infinitely Repeating Decimals May, 2001 Dr. Michael J. Bossé* Department of Mathematics Indiana University of Pennsylvania Indiana, PA 15705 Dr. Fengshan Liu* Department of Mathematics Delaware State University 1200 N duPont Hwy Dover, DE 19901 fliu@dsc.edu (302) 857-6646 Dr. N. R. Nandakumar* Department of Mathematics Delaware State University 1200 N duPont Hwy Dover, DE 19901 nnandaku@dsc.edu (302) 857-6647 * Authors’ names appear in alphabetical order. Exact Computer Calculations With Infinitely Repeating Decimals ABSTRACT Both computers and calculators are limited by architecture, operating system, and software, to some predetermined level of precision within decimal number presentation and calculation. Rational fractions produce either terminating or repeating decimal numbers. In decimal form, rational fractions which produce repeating decimals cannot be represented exactly on computers or calculators. At some decimal place value, the decimal number must be truncated. This process leads to approximations, rather than exact numeric representation and calculation. Exact calculations can only be performed by computers and calculators on terminating decimal numbers. This paper demonstrates that for any rational fraction which produces a repeating decimal, there exists a base into which that number can be converted so that it produces a terminating decimal. Therefore, through converting to appropriate bases computers and calculators can provide exact representations and operations on newly formed terminating fractions. 1 Exact Computer Calculations With Infinitely Repeating Decimals Preliminaries Assignment #1: Use a calculator or a computer to perform the following addition. Provide an exact answer, not an approximation (no rounding or truncating of the decimal is allowed). (Remember that 1.2345 1.234523452345... ) 1.2345 1.23456 Discussion: In decimal form, it is impossible to enter decimal numbers with infinitely repeating cycles into a calculator or computer. At some point, the number must either be rounded or truncated to some predetermined decimal place value. However, this process will only produce an estimate of the exact answer accurate to one digit less than the decimal place value to which the original numbers were truncated. While computer and calculators may inadequately tackle such calculations, some paper and pencil techniques are available to perform such tasks. Technique 1: This technique simply expands each infinitely repeating cycle until the cycles in each number commonly meet at the end of a cycle. In order to consider this we must introduce new notation and operations. Let cl(x) = n be an operation which enumerates the cycle length of a repeating decimal LCM cl 1.2345; cl 1.23456 LCM 4,5 20 . number. Thus cl 1.248 3 and cl 3.5798 4 . In our example, Therefore, in order to accurately calculate the sum presented above, both 1.2345 and 1.23456 must be written to 20 decimal places in order for the cycles to terminate at a common decimal place. This produces: 1.23452345234523452345... 1.23456234562345623456... . Thus, 1.2345 1.23456 2.46808579796869075801 . 2.46908579796869075801... Notably, this sum is again an infinitely repeating decimal cycle and is exact. Technique 2: The numbers discussed within this investigation are of the form of either terminating or repeating decimals. Let us review the two definitions of the rational numbers. 1. Q = {p/q: p, q Z, q 0}, which means that all rational numbers can be expressed as a fraction of integers, with a non-zero denominator. 2. Q = {x| x is a terminating or repeating decimal}. Terminating decimals terminate at some 2 decimal place value. Repeating decimals include some infinitely repeating cycle of digits. A simple procedure allows repeating decimals to be represented as fractions and provides a method which can produce an exact answer to our original problem: 1.2345 1.23456 . We shall first convert these repeating decimals to fractional form. 10,000 x 12,345.2345 x 1.2345 . 9,999 x 12,344.000 100,000 y 123,456.23456 y 1.23456 . 99,999 y 123,455.0000 Thus, x 12,344 . 9,999 Thus, y 123,455 99,999 Since 9,999 = 9(1,111) and 99,999 = 9(11,111), the LCM(9,999; 99,999) = 9(1,111)(11,111). Thus, 12,344 123,455 12,344 11,111 123,455 1,111 274,312,689 . 9,999 99,999 9,999 11,111 99,999 1,111 111,098,889 Discussion: Interestingly, due to the length of the repeating cycles of decimals in Technique 1 and the precision of the fractions in Technique 2, most calculators and computers will not accurately produce these results. However, cumbersome paper and pencil techniques often successfully accomplish tasks difficult or impossible for technology. The question then arises, can technology be modified to accurately handle infinitely repeating decimals? The response is a resounding “yes.” We consider this as follows. Terminating Fractions We define terminating fractions as fractions (rational numbers) which can be rewritten as decimals which terminate. For the remainder of this investigation, we will consider n , where n and d are relatively d prime positive integers. Since this study will consider numeration systems in different bases, b will be a positive integer. In base 10, the portion of the number to the right of the decimal point is commonly named the “decimal” part of the number. To allow generic discussions of base b, this investigation will title the portion of the number to the right of the “fraction point” the “bessimal” part of the base b number. All exponents will be recognized as non-negative integers. Base 10. In base 10, a rational number is defined as 3 n i w 10 w i w1 10 w1 ... ii 10 j ... i 0 f 1 10 1 f 2 10 2 ... f k 10 k ... , d where n and d are relatively prime non-negative integers, and i j and f k are integers less than 10. Can it be predicted, in base 10, which fractions will produce terminating and repeating decimals? We offer here a theorem for base 10 which will be proven in discussions regarding generic base b. Theorem 1. For n m m in lowest form in base 10, if d 2 1 5 2 , where m1 and m2 are nond negative integers, then n d will produce a terminating decimal. All other denominators will result in decimal forms with infinitely repeating cycles. Proof: Special case of Theorem 3. Examples: The following rational numbers produce terminating decimals: 3 3 2 0.15 20 2 5 97 97 4 3 2000 2 5 197 197 0.3152 625 5 4 The following rational numbers produce decimal numbers with infinitely repeating decimal cycles: 37 37 2 0.123 300 2 3 5 2 Theorem 2. For n d 61 61 0.17428571 350 2 5 2 7 9 0.81 11 m m in lowest form in base 10, if d 2 1 5 2 , where m1 and m2 are non- negative integers, the number of decimal places within the decimal number produced by n is max[m1, m2]. d Proof: Special case of Corollary 1. Examples: 49 49 4 2 0.1225 ; max(4, 2) = 4; four decimal places. 400 2 5 17 17 3 6 0.000136 ; max(3, 6) = 6; six decimal places. 125000 2 5 Base b. Definition 1. Let integer b > 0 be the base of a number system. Then a rational number is n i w b w i w1 b w1 ... ii b j ... i 0 f 1 b 1 f 2 b 2 ... f k b k ... d 4 where n and d are relatively prime non-negative integers, i j and f k are integers less than b. Definition 2. A fraction is called a terminating fraction in base b if there is an integer T such that f k = 0 for all k > T in Definition 1. T is called the fraction length. Example: For the hexidecimal number 4.AF36C200000..., 2 is in the position k = 6. However, for all positions such that k > 6, f k = 0. Thus, this number terminates. The following conditions hold for each of the ensuing theorems: n will denote a fraction within a d becimal system where n and d are relatively prime positive integers. Lemma 1. A positive integer multiple of a terminating fraction is also a terminating fraction. Proof obvious. Lemma 2. Given any fraction n , if it terminates in a base b, then it terminates in bases of positive d integer multiple of b. Proof: Since n n terminates in base b, b T must be an integer, where T is the fraction length in d d base b. Let k be a positive integer. Thus kb T therefore, n n k T b T is an integer and, d d n also terminates in base kb. d Theorem 1.Given any fraction n n n n , let d p1 1 p2 2 ps s , where p1, p2, ..., ps are prime factors d with powers n1 , n 2 , n s 1respectively. Then the fraction n is a terminating fraction in a base of a d positive integer multiple of p1 p2 ps . The minimal base for this is p1 p2 ps . Proof: Case 1. n = 1. Let N=max{ n1 , n 2 , n s }. ( p1 p2 ps ) N d p1 N n N n N n1 p2 N n2 ps N ns N ns 1 p1 1 p2 2 ps d ( p1 p2 ps ) N 5 Since p1 p1 N n1 N n1 p2 p2 N n2 N n2 ps ps N ns N ns ( p1 p2 ps ) N , we can write f1 ( p1 p2 ps ) N 1 f N 1 ( p1 p2 ps ) f N where integers f1 ,, f N 1 , f N are between 0 and p1 p2 ps . Therefore, 1 f1 ( p1 p 2 p s ) N 1 f N 1 ( p1 p 2 p s ) f N d ( p1 p 2 p s ) N f N 1 fN f1 . N 1 p1 p 2 p s ( p1 p 2 p s ) ( p1 p 2 p s ) N Case 2. General Case, numerator n is not necessary 1. By Lemma 1, n is also a terminating fraction. By Lemma 2, is also a terminating fraction in bases of d integer multiple of p1 , p2 ,, ps . Examples: bases 1 1 is a terminating fraction in bases 2 3 and 2 2 3 . 3 1 is a terminating fraction in 1 2 3 2 3 1 23 3 and 25 32 . 1 is a terminating fraction in bases 2 3 52 7 3 3 2 2 3 5 7 11and 23 32 52 73 11 . Corollary 1. For terminating fraction n in lowest form in base d p1 , p2 ,, ps , when d d p1 1 , p2 2 ,, ps s , the number of becimal places within the number in becimal form is m m m max[ m1 , m2 ,, ms ] . See proof above. Corollary 2. If there exists some ps qs , then n , produces a bessimal with an infinitely d repeating cycle. Proof: Obvious. Corollary 3. The minimal base for a terminating fraction, bmin p1 p2 p3 ps . 6 n m m m , where d p1 1 , p2 2 ,, ps s , is d Theorem 2. Proof: If n is a terminating bessimal in base b, then d = kb, where k N . d n iw b w iw 1 b w 1 i j b j i0 f1 b 1 f 2 b 2 f k r k d Multiplying both sides by b k produces, bk n d b k (iw b w iw 1 b w 1 i j b j i0 f1 b 1 f 2 b 2 f k r k ) . bk n d iw b w k iw 1 b w k 1 i j b j k i0 b k f1 b k 1 f 2 b k 2 f k Since the right hand side of this equation is an integer, b k divisible by d p1 1 , p2 2 ,, ps m m ms n must also be an integer. Notably, b k is d . Since p1 , p2 ,, ps are all distinct prime numbers, b k must be divisible by p1 , p2 ,, ps . Finally, b must be a multiple of p1 , p2 ,, ps . 1 would be a terminating fraction in any of the following bases: 3 2 4 3 2 3 5 7 Examples: 23 34 54 7 4 11 23 32 54 73 500 25 35 55 7 4 Applications It has been shown that for any rational number, there exists a base b in which fraction. For instance, in base 10 the rational number n will be a terminating d 1 0.3 is a non-terminating decimal, whereas 3 1 0.13 , which is a terminating bessimal. While this paper does not consider techniques for 3 10 converting fractions from one base to another, this investigation will demonstrate how to make the selection of bases for which any n can be translated into a terminating fraction. d Base Selection for Terminating Fractions. Example. Consider the rational number 5 0.416 . 12 demonstrate that any of the following bases would convert 7 2 Since 12 2 3 , the previous theorems 5 into a terminating fraction: 12 23 6; 2 2 3 12 ; 2 2 32 36 ; 22 3 7 84 ; or k 2 m 3n , where k, m, n are positive integers. Assignment #2a. Consider various bases through which adding or multiplying 17 0.283 with 60 69 0.19714285 produces exact results. 350 Since 60 22 3 5 and 350 2 52 7 , bases k1 2m1 3n1 5 p1 ( ka , ma , na , pa , qa are positive integers) are appropriate conversions for Thus, an appropriate base for converting both 17 60 and and k2 2m2 5 p 2 7 q 2 69 respectively. 350 17 69 and to terminating fractions could be 60 350 k3 2m3 3 p3 5 p3 7 q3 , where m3 min( m1 , m2 ), n3 n1, p3 min( p1, p2 ) , and q3 q2 . Assignment #2b. Find the smallest base in which both 17 69 and terminate. 60 350 The smallest base which would be appropriate for this problem would be 2 3 5 7 210 . Computer Applications. Clearly, non-terminating decimal numbers are problematic for computers and calculators. All truncating of decimals produces approximations rather than exact results. However, terminating decimals allow for computers and calculators to produce exact mathematical calculations. This investigation demonstrates that any rational number may be converted into another base in which the bessimal will be terminating. Additionally, although not in the general case, Assignment 2 evidences that an appropriate base can be found to perform exact calculations on multiple rational numbers. While this all seems quite simplistic, the nature of computer architecture introduces additional caveats and concerns. Computer architecture allows for convenient interchange between bases 2, 8, and 16 (and with some additional but easy transformations into any base 2 n , where n is a positive integer). However, algorithms for conversions to other bases are somewhat more complex. Furthermore, while conversions to other bases are possible, they are extremely artificial. Although computers may mimic arithmetic in bases other than 2 n , current technological architecture requires that all mathematics within computers and calculators actually be performed in base 2. Hence, to fully accomplish the conversion of rational numbers into terminating bessimals in other bases, this discussion assumes the existence of a virtual 8 computer which can perform operations in any base. Example. Quadratic equations ax 2 bx c 0 with rational coefficients a, b, and c have rational roots when the discriminant b 2 4ac is a perfect square over rationals. However, since many rational numbers have non-terminating decimal expansions, when calculators and computers are employed to compute the roots using the quadratic formula, derived results are often approximations rather than exact results. Through the discussions above, expressing the coefficients of a quadratic expression in a suitable base and computing the roots in this base using quadratic formula yields exact values. Consider the equation x 2 7 1 x 1 0 . When the roots are computed using a calculator we obtain 7 x=7, 0.143 rounded to 3 decimal places (an approximation). Certainly, the smaller root is not exact. Increasing the accuracy to more decimal place values does not produce exact results and only continues to produce approximations. However, by expressing the coefficients in base 7 we obtain, x 2 10.17 x 17 0 . Thus, b b 2 4ac 2a 10.1 10.12 4 2 x 7 10.1 102.1 4 2 7 10.1 65.01 2 10.1 6.6 2 7 Hence, 10.1 6.6 10.1 6.6 x , 2 2 7 7 20.0 0.2 , 2 7 2 7 107 , 0.17 1 7 10 , 7 10 1 7 Therefore, although a computer could not calculate exactly the roots of x 2 7 x 1 0 , exact 9 roots were found for x 2 10.17 x 17 0 . Thus, by converting from one base to another, exact mathematics can be readily performed and inherent technological weaknesses can be averted. Background Reading Dickson, L. E. (1934). History of the Theory of Numbers, Volume I, Divisibility and Primality. New York: G. E. Stechert & Co. Mathews, J. H. & Fink, K. D. (1999). Numerical Methods Using MATLAB (3rd Ed.). Upper Saddlr River, NJ: Prentice Hall 10