-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: Fix in classifier of Development status Fix rendering of long description on PyPi Fix missing setuptools import in setup.py, add manifest file Add setup.py file Add instrallation instructions, BSD license and version number Add changelog and readme with example usage and lib description Fixes in docstrings, add file-level docstrings, add example usage tests Add functions to comput convergents of a given value Add .editorconfig used for this project Add PyCharm inspection warnings Implement computation of a continued fraction
- Loading branch information
Showing
9 changed files
with
524 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[*] | ||
charset=utf-8 | ||
end_of_line=lf | ||
trim_trailing_whitespace=true | ||
insert_final_newline=true | ||
indent_style=space | ||
indent_size=4 | ||
|
||
[*.md] | ||
# A double trailing whitespace in Markdown indicates a newline `<br/>`. | ||
trim_trailing_whitespace=false |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Changelog | ||
=============================================================================== | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on | ||
[Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to | ||
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
|
||
[1.0.0] - 2019-04-13 | ||
---------------------------------------- | ||
|
||
Initial version. | ||
|
||
|
||
### Added | ||
|
||
- Continued fractions of `int`, `float`, | ||
`fractions.Fraction` and rational numbers expressed as tuples of 2 integers | ||
`(numerator, denominator)` | ||
- Convergents of the same data types | ||
- Evaluate finite continued fraction into a value | ||
- Arithmetical expression as string of a continued fraction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
BSD 3-Clause License | ||
=============================================================================== | ||
|
||
Copyright © 2019, Matjaž Guštin <dev@matjaz.it> <https://matjaz.it>. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
3. Neither the name of nor the names of its contributors may be used to endorse | ||
or promote products derived from this software without specific prior | ||
written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS “AS IS” AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, | ||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include *.md | ||
include test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.