ContFrac


NameContFrac JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/TheMatjaz/contfrac
SummaryContinued fractions and convergents
upload_time2019-04-13 15:24:24
maintainer
docs_urlNone
authorMatjaž Guštin
requires_python>=3
licenseBSD
keywords continued fraction fractions convergent rational
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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
----------------------------------------

```bash
pip install contfrac
```

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


Example usage
----------------------------------------

```python
>>> 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
----------------------------------------

- [Continued](https://github.com/MostAwesomeDude/continued), also available
  through `pip` 



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/TheMatjaz/contfrac",
    "name": "ContFrac",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "continued,fraction,fractions,convergent,rational",
    "author": "Matja\u017e Gu\u0161tin",
    "author_email": "dev@matjaz.it",
    "download_url": "https://files.pythonhosted.org/packages/d4/cf/cecbf51a968313ee6e7ed438005c348e91ad27747f189560e9ff7ba9d9d3/ContFrac-1.0.0.tar.gz",
    "platform": "",
    "description": "ContFrac\n===============================================================================\n\nContinued fractions are a representation of numbers expressed as recursive\nsums of integer parts and reciprocals of other numbers. _ContFrac_ is a\npure-Python3 lightweight module to compute and evaluate continued fractions,\nas well as using them to approximate any number.  \n\n\nFeatures\n----------------------------------------\n\n- Supports conversion into continued fractions of `int`, `float`,\n  `fractions.Fraction` and rational numbers expressed as tuples of 2 integers\n  `(numerator, denominator)`, generated iteratively.\n- Computes the convergents of the same data types, generated iteratively.\n- Computes the value of a finite continued fraction.\n- Generates the arithmetical expression as string of a continued fraction.\n\n\nInstallation\n----------------------------------------\n\n```bash\npip install contfrac\n```\n\nor just include the `contfrac.py` file in your project (copy-paste).\n\n\nExample usage\n----------------------------------------\n\n```python\n>>> import contfrac\n>>> value = 415/93  # Express as (415, 93) to avoid rounding continued frac.\n>>> coefficients = list(contfrac.continued_fraction(value))\n>>> print(coefficients)\n[4, 2, 6, 7]\n\n>>> expression = contfrac.arithmetical_expr(coefficients)\n>>> print('Value: {:f} = {:s}'.format(value, expression))\nValue: 4.462366 = 4 + 1/(2 + 1/(6 + 1/(7)))\n\n>>> # The evaluation of a float value from a continued fraction is subject\n>>> # to floating point rounding errors\n>>> eval_value = contfrac.evaluate(coefficients)\n>>> print(eval_value, value)  # Visible rounding errors\n4.46236559139785 4.462365591397849\n\n>>> convergents = list(contfrac.convergents(value))\n>>> print(convergents)\n[(4, 1), (9, 2), (58, 13), (415, 93)]\n\n>>> import math\n>>> coefficients = list(contfrac.continued_fraction(math.e, maxlen=10))\n>>> print(coefficients)\n[2, 1, 2, 1, 1, 4, 1, 1, 6, 1]\n\n>>> convergent = contfrac.convergent(math.e, 3)  # Low convergent grade\n>>> print(convergent, convergent[0]/convergent[1], math.e)\n(11, 4) 2.75 2.718281828459045\n\n>>> convergent = contfrac.convergent(math.e, 7)  # Higher grade = more accurate\n>>> print(convergent, convergent[0]/convergent[1], math.e)\n(193, 71) 2.7183098591549295 2.718281828459045\n```\n\n\nSimilar libraries\n----------------------------------------\n\n- [Continued](https://github.com/MostAwesomeDude/continued), also available\n  through `pip` \n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Continued fractions and convergents",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/TheMatjaz/contfrac"
    },
    "split_keywords": [
        "continued",
        "fraction",
        "fractions",
        "convergent",
        "rational"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6778805769b3db8e572bd0ba58f402dea4e2375aec4ec5bbd10e81df65674145",
                "md5": "c42922eb0743603a9ac707e833212457",
                "sha256": "9522b06413115147618256dae6b21801320cb8d24a274da191f0e57154fb8781"
            },
            "downloads": -1,
            "filename": "ContFrac-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c42922eb0743603a9ac707e833212457",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 5781,
            "upload_time": "2019-04-13T15:24:21",
            "upload_time_iso_8601": "2019-04-13T15:24:21.671540Z",
            "url": "https://files.pythonhosted.org/packages/67/78/805769b3db8e572bd0ba58f402dea4e2375aec4ec5bbd10e81df65674145/ContFrac-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d4cfcecbf51a968313ee6e7ed438005c348e91ad27747f189560e9ff7ba9d9d3",
                "md5": "065cacb021f610c1f3c91e100ef42b16",
                "sha256": "e22296506f636ccb8da8142f41ca2dafe275e09b5bbd37df1c77b93e2675ee29"
            },
            "downloads": -1,
            "filename": "ContFrac-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "065cacb021f610c1f3c91e100ef42b16",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 7404,
            "upload_time": "2019-04-13T15:24:24",
            "upload_time_iso_8601": "2019-04-13T15:24:24.167306Z",
            "url": "https://files.pythonhosted.org/packages/d4/cf/cecbf51a968313ee6e7ed438005c348e91ad27747f189560e9ff7ba9d9d3/ContFrac-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-04-13 15:24:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TheMatjaz",
    "github_project": "contfrac",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "contfrac"
}
        
Elapsed time: 0.21023s