Continued Fraction Calculator
Convert decimals or fractions to continued fraction notation with convergents.
About this tool
A continued fraction is a mathematical representation that expresses any real number as a sequence of nested divisions. Unlike standard decimal notation, continued fractions reveal the underlying arithmetic structure of a number and provide progressively better rational approximations. This tool converts your input—whether a decimal like 3.14159 or a fraction like 355/113—into its continued fraction form, making it easy to understand how mathematicians have historically discovered remarkably accurate rational approximations to irrational constants.
Using this calculator is straightforward: enter your decimal number or a fraction in the form numerator/denominator, then specify how many terms you want to compute. The tool displays the continued fraction notation in the form [a₀; a₁, a₂, ...] and generates a table of convergents—the sequence of fractions that progressively approximate your original number with increasing accuracy. Each convergent provides the best possible rational approximation relative to its denominator size, a property exploited in navigation systems, astronomical calculations, and music tuning.
Continued fractions are particularly valuable in number theory and cryptography, where approximating irrational numbers efficiently can expose subtle mathematical properties. The periodic patterns that emerge for certain irrational numbers (like the square root of 2) reveal deep connections to algebra and geometry. Whether you're exploring pure mathematics, validating engineering approximations, or simply curious about why 355/113 approximates π so accurately, this tool transforms abstract continued fraction theory into visual, interactive computation.
Frequently Asked Questions
Code Implementation
import math
def continued_fraction(x: float, max_terms: int = 20) -> list[int]:
"""Compute continued fraction representation of x"""
terms = []
remaining = x
for _ in range(max_terms):
a = int(math.floor(remaining))
terms.append(a)
frac = remaining - a
if abs(frac) < 1e-10:
break
remaining = 1 / frac
if not math.isfinite(remaining):
break
return terms
def convergents(terms: list[int]) -> list[tuple[int, int]]:
"""Compute convergents (rational approximations) from continued fraction terms"""
conv = []
h_prev, h_curr = 1, terms[0]
k_prev, k_curr = 0, 1
conv.append((h_curr, k_curr))
for i in range(1, len(terms)):
a = terms[i]
h_next = a * h_curr + h_prev
k_next = a * k_curr + k_prev
h_prev, h_curr = h_curr, h_next
k_prev, k_curr = k_curr, k_next
conv.append((h_curr, k_curr))
return conv
# Example: pi
pi_terms = continued_fraction(math.pi, 10)
print(f"pi = [{pi_terms[0]}; {', '.join(map(str, pi_terms[1:]))}]")
for num, den in convergents(pi_terms):
approx = num / den
error = abs(approx - math.pi)
print(f" {num}/{den} = {approx:.10f} (error: {error:.2e})")
# Golden ratio
phi = (1 + math.sqrt(5)) / 2
print(f"phi terms: {continued_fraction(phi, 8)}") # All ones!
# sqrt(2)
sqrt2_terms = continued_fraction(math.sqrt(2), 8)
print(f"sqrt(2) terms: {sqrt2_terms}") # [1; 2, 2, 2, ...]
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.