Skip to main content

Continued fractions and convergents

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ContFrac-1.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

ContFrac-1.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file ContFrac-1.0.0.tar.gz.

File metadata

  • Download URL: ContFrac-1.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ContFrac-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e22296506f636ccb8da8142f41ca2dafe275e09b5bbd37df1c77b93e2675ee29
MD5 065cacb021f610c1f3c91e100ef42b16
BLAKE2b-256 d4cfcecbf51a968313ee6e7ed438005c348e91ad27747f189560e9ff7ba9d9d3

See more details on using hashes here.

File details

Details for the file ContFrac-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ContFrac-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ContFrac-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9522b06413115147618256dae6b21801320cb8d24a274da191f0e57154fb8781
MD5 c42922eb0743603a9ac707e833212457
BLAKE2b-256 6778805769b3db8e572bd0ba58f402dea4e2375aec4ec5bbd10e81df65674145

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page