Algorithm To Convert A Decimal To A Fraction

Similar documents
6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Intro. Iterators. 1. Access

CS1100 Introduction to Programming

Life Tables (Times) Summary. Sample StatFolio: lifetable times.sgp

Sum of Linear and Fractional Multiobjective Programming Problem under Fuzzy Rules Constraints

Mathematics 256 a course in differential equations for engineering students

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Data Representation in Digital Design, a Single Conversion Equation and a Formal Languages Approach

The Codesign Challenge

GSLM Operations Research II Fall 13/14

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

Improving Low Density Parity Check Codes Over the Erasure Channel. The Nelder Mead Downhill Simplex Method. Scott Stransky

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

K-means and Hierarchical Clustering

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Programming in Fortran 90 : 2017/2018

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

Optimization Methods: Integer Programming Integer Linear Programming 1. Module 7 Lecture Notes 1. Integer Linear Programming

Searching & Sorting. Definitions of Search and Sort. Linear Search in C++ Linear Search. Week 11. index to the item, or -1 if not found.

9. BASIC programming: Control and Repetition

An Optimal Algorithm for Prufer Codes *

Agenda & Reading. Simple If. Decision-Making Statements. COMPSCI 280 S1C Applications Programming. Programming Fundamentals

A Clustering Algorithm for Chinese Adjectives and Nouns 1

NUMERICAL SOLVING OPTIMAL CONTROL PROBLEMS BY THE METHOD OF VARIATIONS

CHAPTER 2 DECOMPOSITION OF GRAPHS

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

Problem Set 3 Solutions

Range images. Range image registration. Examples of sampling patterns. Range images and range surfaces

Unsupervised Learning and Clustering

5.1 The ISR: Overvieui. chapter

VISUAL SELECTION OF SURFACE FEATURES DURING THEIR GEOMETRIC SIMULATION WITH THE HELP OF COMPUTER TECHNOLOGIES

CS 534: Computer Vision Model Fitting

2x x l. Module 3: Element Properties Lecture 4: Lagrange and Serendipity Elements

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Module Management Tool in Software Development Organizations

Machine Learning. Topic 6: Clustering

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

Solutions to Programming Assignment Five Interpolation and Numerical Differentiation

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

S1 Note. Basis functions.

CMPS 10 Introduction to Computer Science Lecture Notes

Non-Split Restrained Dominating Set of an Interval Graph Using an Algorithm

Lecture 3: Computer Arithmetic: Multiplication and Division

Loop Transformations, Dependences, and Parallelization

Conditional Speculative Decimal Addition*

Intra-Parametric Analysis of a Fuzzy MOLP

Ramsey numbers of cubes versus cliques

CE 221 Data Structures and Algorithms

Random Kernel Perceptron on ATTiny2313 Microcontroller

Motivation. EE 457 Unit 4. Throughput vs. Latency. Performance Depends on View Point?! Computer System Performance. An individual user wants to:

ETAtouch RESTful Webservices

Solitary and Traveling Wave Solutions to a Model. of Long Range Diffusion Involving Flux with. Stability Analysis

Machine Learning: Algorithms and Applications

Brave New World Pseudocode Reference

MATHEMATICS FORM ONE SCHEME OF WORK 2004

AP PHYSICS B 2008 SCORING GUIDELINES

ELEC 377 Operating Systems. Week 6 Class 3

Report on On-line Graph Coloring

5 The Primal-Dual Method

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Classification / Regression Support Vector Machines

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

U.C. Berkeley CS294: Beyond Worst-Case Analysis Handout 5 Luca Trevisan September 7, 2017

Array transposition in CUDA shared memory

Specifications in 2001

USING GRAPHING SKILLS

Pass by Reference vs. Pass by Value


CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Solving two-person zero-sum game by Matlab

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Support Vector Machines. CS534 - Machine Learning

A Geometric Approach for Multi-Degree Spline

LLVM passes and Intro to Loop Transformation Frameworks

Random Variables and Probability Distributions

Machine Learning 9. week

DESIGN OF VERTICAL ALIGNMET

Installation and User Guide. Digidim Remote Control (303) Product description. Switching Lights On/Off using Digidim 303 Remote

Midterms Save the Dates!

Introduction to Geometrical Optics - a 2D ray tracing Excel model for spherical mirrors - Part 2

Quality Improvement Algorithm for Tetrahedral Mesh Based on Optimal Delaunay Triangulation

Discrete Applied Mathematics. Shortest paths in linear time on minor-closed graph classes, with an application to Steiner tree approximation

A New Approach For the Ranking of Fuzzy Sets With Different Heights

ATYPICAL SDN consists of a logical controller in the

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

LEAST SQUARES. RANSAC. HOUGH TRANSFORM.

Analysis of Collaborative Distributed Admission Control in x Networks

A Five-Point Subdivision Scheme with Two Parameters and a Four-Point Shape-Preserving Scheme

Lecture 5: Probability Distributions. Random Variables

A Fast Visual Tracking Algorithm Based on Circle Pixels Matching

An Application of Network Simplex Method for Minimum Cost Flow Problems

Recognizing Faces. Outline

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

A Facet Generation Procedure. for solving 0/1 integer programs

Graph-Theoretic Methods

A Binarization Algorithm specialized on Document Images and Photos

Parallel matrix-vector multiplication

Transcription:

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