Continued fractions is an alternative representation of numbers that has interesting properties for on-demand arbitrary precision while avoiding intermediary rounding errors.
Questions tagged [continued-fractions]
33 questions
17
votes
2 answers
Algorithm Challenge: Generate Continued Fractions for a float
(EDIT: In response to grumpy comments, No it isn't homework. I am working on pitch detection, taking an array of potential harmonic peaks, and attempting to construct candidates for fundamental frequency. So, it is actually a very practical…

P i
- 29,020
- 36
- 159
- 267
8
votes
1 answer
Exact value of a floating-point number as a rational
I'm looking for a method to convert the exact value of a floating-point number to a rational quotient of two integers, i.e. a / b, where b is not larger than a specified maximum denominator b_max. If satisfying the condition b <= b_max is…

plasmacel
- 8,183
- 7
- 53
- 101
6
votes
2 answers
Continued Fractions Python
I am new to Python and was asked to create a program that would take an input as a non-negative integer n and then compute an approximation for
the value of e using the first n + 1 terms of the continued fraction:
I have attempted to decipher the…

Timothy Kardaras
- 123
- 1
- 1
- 8
5
votes
3 answers
Python 2.7 - Continued Fraction Expansion - Understanding the error
I've written this code to calculate the continued fraction expansion of a rational number N using the Euclidean algorithm:
from __future__ import division
def contFract(N):
while True:
yield N//1
f = N - (N//1)
if f ==…

ggordon
- 259
- 1
- 3
- 16
4
votes
2 answers
Arbitrary Precision Arithmetic in Julia
This has kinda been asked, but not in this way. I have a little Python program which finds continued fractions for square roots of n (1 <= n <= 10000).
I have been trying to do this in Julia and I can't see how to. Mainly because it deals with…

davo36
- 694
- 9
- 19
3
votes
3 answers
Is it possible to calculate the continued fraction of a negative number in f#?
I'm trying to make a program that can calculate the calculated fraction of a real number.
It works completely fine except when I'm trying to do it for a negative real number, ex "-71/23" or in decimals "-3,086...".
If I calculate the continued…

Zebraboard
- 210
- 1
- 13
3
votes
1 answer
Continued logarithm arithmetic: floor operator on run-length encoded terms
I'm trying to implement basic arithmetic on Bill Gosper's continued logarithms, which are a 'mutation' of continued fractions allowing the term co-routines to emit and consume very small messages even on very large or very small numbers.
Reversible…
user2875414
3
votes
2 answers
Finding the continued fraction of 2^(1/3) to very high precision
Here I'll use the notation
It is possible to find the continued fraction of a number by computing it then applying the definition, but that requires at least O(n) bits of memory to find a0, a1 ... an, in practice it is a much worse. Using double…

Sophie
- 131
- 2
3
votes
5 answers
Good compression scheme for continued fraction terms?
So I'm implementing a continued fraction library for handling a subset of quadratic integers and rational numbers. Continued fraction terms are represented by unsigned integers. I've noticed the following general patterns when working with…

hatch22
- 797
- 6
- 18
2
votes
1 answer
MATLAB gives more precision than expected when approximating pi
I am using 2021b. I wanted to look at the convergence rate of a iteration algorithm for Pi, using continued fraction. The formula was for wikipedia https://en.wikipedia.org/wiki/Pi#Continued_fractions.
by iteration 10 steps,precision up to 6 digits…

N.Li
- 47
- 7
2
votes
1 answer
How to calculate the terms of the continued fraction of pi?
The other day, the Wolfram Blog published an article about a thirteen year old boy, Neil Bickford, who computed the first 458 million terms of the simple continued fraction representation of pi, beginning with [3; 7, 15, 1, 292, ...]. Bickford…

user448810
- 17,381
- 4
- 34
- 59
2
votes
1 answer
Square root calculation using continued fractions to n bits of precision
This is an unsolved problem from my past arbitrary-precision rational numbers C++ assignment.
For calculation, I used this expression from Wikipedia (a being the initial guess, r being its remainder):
I ended up, just by guessing from experiments,…

Juraj Fiala
- 170
- 1
- 12
2
votes
1 answer
implementing an algorithm to transform a real number to a continued fraction in #F
i am trying to implement a recursive function 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…

sss
- 115
- 6
2
votes
1 answer
Why the bleep isn't my continued fraction approximating properly?
Reading through more SICP and I'm stuck on exercise 1.3.8. My code works properly for approximating 1/phi, but doesn't work for approximating e - 2.
(define (cont-frac n d k)
(define (frac n d k)
(if (= k 0)
1.0
(+…

afkbowflexin
- 4,029
- 2
- 37
- 61
1
vote
1 answer
How to write python code to plot the process of the Euclidean algorithm on a rectangle
I am a new coder and want to write some code to plot various rectangles in matplotlib, taking the animation from this link to demonstrate continued fractions. Namely, I am trying to write a function that takes the width and height of a rectangle and…

goodlander01
- 13
- 4