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
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:
let rec float2cfrac (x : float) : int list = let q = int x let r = x - (float q) if r = 0.0 then [] else q :: (float2cfrac (1.0 / r ))
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
