Skip to main content

ContFrac

Continued fractions are a representation of numbers expressed as recursive sums of integer parts and reciprocals of other numbers. ContFrac is a pure-Python3 lightweight module to compute and evaluate continued fractions, as well as using them to approximate any number.

Features

  • Supports conversion into continued fractions of int, float, fractions.Fraction and rational numbers expressed as tuples of 2 integers (numerator, denominator), generated iteratively.
  • Computes the convergents of the same data types, generated iteratively.
  • Computes the value of a finite continued fraction.
  • Generates the arithmetical expression as string of a continued fraction.

Installation

pip install contfrac

or just include the contfrac.py file in your project (copy-paste).

Example usage

>>> import contfrac
>>> value = 415/93  # Express as (415, 93) to avoid rounding continued frac.
>>> coefficients = list(contfrac.continued_fraction(value))
>>> print(coefficients)
[4, 2, 6, 7]

>>> expression = contfrac.arithmetical_expr(coefficients)
>>> print('Value: {:f} = {:s}'.format(value, expression))
Value: 4.462366 = 4 + 1/(2 + 1/(6 + 1/(7)))

>>> # The evaluation of a float value from a continued fraction is subject
>>> # to floating point rounding errors
>>> eval_value = contfrac.evaluate(coefficients)
>>> print(eval_value, value)  # Visible rounding errors
4.46236559139785 4.462365591397849

>>> convergents = list(contfrac.convergents(value))
>>> print(convergents)
[(4, 1), (9, 2), (58, 13), (415, 93)]

>>> import math
>>> coefficients = list(contfrac.continued_fraction(math.e, maxlen=10))
>>> print(coefficients)
[2, 1, 2, 1, 1, 4, 1, 1, 6, 1]

>>> convergent = contfrac.convergent(math.e, 3)  # Low convergent grade
>>> print(convergent, convergent[0]/convergent[1], math.e)
(11, 4) 2.75 2.718281828459045

>>> convergent = contfrac.convergent(math.e, 7)  # Higher grade = more accurate
>>> print(convergent, convergent[0]/convergent[1], math.e)
(193, 71) 2.7183098591549295 2.718281828459045

Similar libraries

Metadata

Release files for ContFrac 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ContFrac 1.0.0
File Size Uploaded
ContFrac-1.0.0.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ContFrac 1.0.0
File Interpreter ABI Platform
ContFrac-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.2 kB

Release files / ContFrac-1.0.0.tar.gz

Download URL ContFrac-1.0.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e22296506f636ccb8da8142f41ca2dafe275e09b5bbd37df1c77b93e2675ee29
BLAKE2b-256 checksum
How to use checksums
d4cfcecbf51a968313ee6e7ed438005c348e91ad27747f189560e9ff7ba9d9d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release files / ContFrac-1.0.0-py3-none-any.whl

Download URL ContFrac-1.0.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9522b06413115147618256dae6b21801320cb8d24a274da191f0e57154fb8781
BLAKE2b-256 checksum
How to use checksums
6778805769b3db8e572bd0ba58f402dea4e2375aec4ec5bbd10e81df65674145
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page