Skip to content
Omnikalc

Continued fraction calculator: find the best fraction for any number

Enter a number and the number of terms to get its continued fraction coefficients and the matching fraction, with its error.

Updated on
Your inputs

Value out of range

Value out of range

Results

Numerator of the fraction

355

Denominator of the fraction

113

Coefficient a0
3
Coefficient a1
7
Coefficient a2
15
Coefficient a3
1
Coefficient a4
292
Coefficient a5
1
Value of the fraction
3.141592920
Error (fraction minus number)
0.000000267

Every number can be turned into a ladder of whole numbers that leads to better and better fractions. The continued fraction calculator takes a number and the number of terms to use, and gives the coefficients a0 to a5, the numerator and denominator of the fraction built from those terms, the value of the fraction and the error compared with the number you entered.

How the calculation works

  • Enter a positive number, such as 3.14159265358979 for pi or 1.41421356 for the square root of 2.
  • Choose how many terms to use for the fraction, from 1 to 6. A term is one step of the expansion.
  • The calculator takes the whole part a0, then takes the inverse of what is left, and repeats: a1 is the whole part of that inverse, and so on. A coefficient of 0 after the last term means that the expansion has ended.
  • The fraction is built with the recurrence p(n) = a(n) × p(n−1) + p(n−2) and q(n) = a(n) × q(n−1) + q(n−2). It is the best fraction you can write with a denominator that size.

Example

Take π = 3.14159265358979. The whole part is 3, and the inverse of what remains is 7.0625, so a1 = 7. Then a2 = 15, a3 = 1, a4 = 292 and a5 = 1. With 4 terms the fraction is 355/113 = 3.14159292, off by only 2.7 × 10⁻⁷. With 5 terms, it is 103993/33102, and with 2 terms it is the old 22/7. The big coefficient 292 explains why 355/113 is so close.

Why it matters

Continued fractions give the best fraction for a given size of denominator. Gear makers use them to find tooth counts that give a ratio, calendar makers use them to match the length of a year, and musicians use them to find simple interval approximations. They are also a window into number theory.

Good to know

Inputs are decimals, so the expansion of a number such as 1/3 entered as 0.3333 will not end where the exact fraction would. Rounding in the input changes the later coefficients, so enter as many digits as you know. The fraction shown is reduced by the construction, so no further simplification is needed.

The Fibonacci calculator is linked to the golden ratio, whose continued fraction is all ones, the golden ratio calculator gives the number itself, the geometric mean calculator helps with ratios, and the binomial expansion calculator covers another expansion of numbers.

Frequently asked questions

What is a continued fraction?

It is a way to write a number as a whole part plus the inverse of another number, which is itself written the same way. The list of whole parts, written [a0; a1, a2, ...], identifies the number. Rational numbers end after a finite number of terms, and irrational numbers go on forever.

What are convergents?

They are the fractions you get by stopping the expansion after one, two, three, and more terms. Each is the best approximation of the number among all fractions with a denominator no larger, which is why 22/7 and 355/113 are famous for pi. The error shrinks quickly, especially just before a large coefficient.

Why do famous numbers have special patterns?

The golden ratio has all coefficients equal to 1, which makes it the hardest number to approximate by fractions. The square root of 2 repeats 2 forever. Euler's number e follows a regular pattern of 1, 2k, 1. Pi has no known pattern, with a surprise 292 in fifth place.

How exact are the results?

The calculator works in standard double precision, so the first five or six terms are reliable for a number given to 14 or more digits. Terms beyond that are affected by rounding. If you enter a short decimal such as 0.75, the expansion ends early, and the calculator stops there.

Was this calculator helpful?

Thanks for your feedback!

Results are estimates provided for information purposes. They do not replace professional advice.