Ask Question up vote0down votefavorite i am trying to implement a recursive func
ID: 3731439 • Letter: A
Question
Ask Question
up vote0down votefavorite
i am trying to implement a recursive function in F# which takes a float and returns a list of ints representing the continued fraction representation of the float (https://en.wikipedia.org/wiki/Continued_fraction) In general i think i understand how the algorithm is supposed to work. its fairly simply. What i have so far is this:
the problem is with the base case obviously. It seems the value r never does reduce to 0.0 instead the algorithm keeps on returning values which are the likes of 0.0.....[number]. I am just not sure how to perform the comparison. How exactly should i go about it. The algorithm the function is based on says the base case is 0, so i naturally interpret this as 0.0. I dont see any other way. Also, do note that this is for an assignment where i am explicitly asked to implement the algorithm recursively. Does anyone have some guidance for me? It would be much appreciated
Explanation / Answer
The problem is that you can not just compare
r == 0.0
because in case of floating point numbers they may never converge to 0.0 instead value might be 0.0000000001. Very close to zero but not actually zero.
So a very simple solution is to define a
and compare
r < EPSILON instead of r == 0.0
Here is your modified code
Choose your epsison wisely.
You can print value of r and see what is the minumum value of r is and then choose epsilon.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.