Algorthm To Convert A ecmal To A Fracton by John Kennedy Mathematcs epartment Santa Monca College 1900 Pco Blvd. Santa Monca, CA 90405 jrkennedy6@gmal.com
Except for ths comment explanng that t s blank for some delberate reason, ths page s ntentonally blank!
CONVERTING ECIMALS TO FRACTIONS Let X denote the orgnal decmal. In the followng algorthm descrpton we assume X 0. In the code example we take nto account the cases where X œ 0.0 or where X 0.0 or where X s already an exact nteger. We defne two recursve sequences, Z and and we defne one non-recursve sequence N. The fractons N wll approxmate the orgnal decmal X. In fact, these fractons oscllate below and above X and converge to X. The sequence Z s related to the contnued fracton approxmaton to X and s otherwse used only to help fnd the whch are the mportant values that the algorthm fnds and returns. The sequences Z and are ntalzed wth the followng values for œ 0 and œ 1. Z 0 s undefned. Z 1 œ X 0 œ 0 and 1 œ 1 For œ 1, 2, 3,... we calculate the followng values n the order Z 1 frst, then 1, and fnally N 1 as shown below. 1 1 Z Int ÐZ Ñ Z œ Int ÐÑ = nteger part functon œ Int ( Z Ñ 1 1 1 N œ Round Ð X Ñ 1 1 Round ÐÑ= rounds to the nearest nteger. Note that once s accurately known, the correspondng N value s trval to fnd. The real value of the algorthm s n specfyng the calculaton of the sequence. 5 Frst Example: X œ œ 0.263157894737 19 Z N N 0 ------------------ ----- 0 ------------------ 1 0.263157894737 0 1 0.00000000000 2 3.8 1 3 0.33333333333 3 1.25 1 4 0.25000000000 4 4. 5 19 0.263157894737 5 undefned! ------- ------ ------------------ Convertng ecmals To Fractons 1
Second Example: X œ 1 œ 3.14159265359 Z N N 0 ------------------ ----- 0 ------------------ 1 3.14159265359 3 1 3.00000000000 2 7.06251330592 22 7 3.14285714286 3 15.9965944095 333 106 3.14150943396 4 1.00341722818 355 113 3.14159292035 5 292.63483365 103993 33102 3.14159265301 6 1.57521580653 104348 33215 3.14159265392 7 1.7384779567 208341 66317 3.14159265347 8 1.35413656011 312689 99532 3.14159265362 9 2.82376945122 833719 265381 3.14159265358 10 1.21393188169 1146408 364913 3.14159265359 11 4.67438509913 4585632 1459652 3.14159265359 For ths example, the more terates that are made, the larger the numerators and denomnators of the approxmatng fractons. Snce there s no change between the last two fracton approxmatons ( when the fractons are converted back to decmals they yeld the same decmal values whch appear n the rghtmost column ) the algorthm can be stopped after the 11th step. 37 Thrd Example: X œ œ 0.606557377049 61 Z N N 0 ------------------ ----- 0 ------------------ 1 0.606557377049 0 1 0.00000000000 2 1.64864864865 0 1 0.00000000000 3 1.54166666666 1 2 0.50000000000 4 1.84615384618 2 3 0.66666666666 5 1.18181818181 3 5 0.60000000000 6 5.5 17 28 0.607142857143 7 2. 37 61 0.606557377049 8 undefned! ------- ------ ------------------ Convertng ecmals To Fractons 2
The followng code fragment s Turbo Pascal code that converts a decmal to a sngle fracton. When convertng ths code fragment to another language the followng remarks may be helpful. The extended data type can be replaced by any floatng pont or real number data type. The Abs functon s the Absolute Value functon. The Int functon s the Integer Part functon. For example, Int Ð3.75 Ñ œ 3 and Int Ð 2.3 Ñ œ 2. The varable Z s used to represent the above sequence varable Z. The varable FractonNumerator s used to represent the above sequence varable N. The varable Fractonenomnator s used to represent the above sequence varable. The varable Prevousenomnator s used to represent the above sequence varable 1. The value of AccuracyFactor s used to determne how accurate the converson needs to be. For example, f AccuracyFactor œ 0.0005 then the converson should be accurate to 3 decmal places. To get accuracy to 5 decmal places set the AccuracyFactor œ 0.000005. The hgher the AccuracyFactor the larger but more accurate s the fracton that s returned. The code that executes frst saves the sgn of X and then takes the absolute value so the algorthm really only works on nonnegatve decmals. The frst test checks f X s already an exact whole number. In ths case the denomnator s set to 1 and the procedure termnates mmedately. Note that ths case ncludes the possblty that X œ 0. Next, the code checks to see f the decmal s smaller than the smallest representable fracton. If so, the smallest representable fracton s returned. Note that f X=0 the f-statement test would fal to take ths case nto account, but we have already handled the case where X œ 0. Zero s a specal case of the truly smallest representable fracton. So we really mean the smallest nonzero representable fracton! Next t checks f the decmal s larger than the largest representable fracton. If so, the largest representable fracton s returned. Falng the above 3 checks, the algorthm fnally begns by gong nto an teraton loop n whch the real work s done. Ths loop s guaranteed to execute at least once. The value AccuracyFactor helps determne when to stop wth the current fracton approxmaton. We must also stop f and when Z becomes an exact nteger. Convertng ecmals To Fractons 3
procedure ecmaltofracton (ecmal : extended; var FractonNumerator : extended; var Fractonenomnator : extended; AccuracyFactor : extended ); var ecmalsgn : extended; Z : extended; Prevousenomnator : extended; ScratchValue : extended; begn f ecmal < 0.0 then ecmalsgn := 1.0 else ecmalsgn := 1.0; ecmal := Abs (ecmal); f ecmal=int (ecmal) then Ö handles exact ntegers ncludng 0 begn FractonNumerator := ecmal*ecmalsgn; Fractonenomnator := 1.0; Ext end; f (ecmal < 1.0E 19) then Ö X œ 0 already taken care of begn FractonNumerator := ecmalsgn; Fractonenomnator := 9999999999999999999.0; Ext end; f (ecmal > 1.0E 19) then begn FractonNumerator := 9999999999999999999.0*ecmalSgn; Fractonenomnator := 1.0; Ext end; Z := ecmal; Prevousenomnator := 0.0; Fractonenomnator := 1.0; repeat Z := 1.0/(Z Int(Z)); ScratchValue := Fractonenomnator; Fractonenomnator := Fractonenomnator*Int(Z)+Prevousenomnator; Prevousenomnator := ScratchValue; FractonNumerator := Int(ecmal*Fractonenomnator + 0.5) { Roundng Functon untl (Abs((ecmal (FractonNumerator/Fractonenomnator))) AccuracyFactor) OR (Z = Int(Z)); FractonNumerator := ecmalsgn*fractonnumerator end; {procedure ecmaltofracton} Convertng ecmals To Fractons 4