PyFract
A simple implementation of fraction numbers in Python. In some cases it is better than default Python library for fractions.
Instalation
pip install pyfract
Running tests
chmod +x run-tests.sh
./run-tests.sh
Usage
Initialization
from pyfract.fraction import Fraction
f = Fraction(1, 2)
print(f) # [1/2]
f = Fraction(numerator=1, denominator=2)
print(f) # [1/2]
Mathematical operations
You can use all of the math operations with other fraction, int or float as shown in the example below
from pyfract.fraction import Fraction
f1 = Fraction(1, 2)
f2 = Fraction(1, 4)
print(f1 + f2) # [3/4]
print(f1 - f2) # [1/4]
print(f1 * f2) # [1/8]
print(f1 / f2) # [2/1]
print(f1 + 2) # [5/2]
print(f1 - 2) # [-3/2]
print(f1 * 2) # [1/1]
print(f1 / 2) # [1/4]
Comparisons
You can compare fractions using operators like greater than, less than and so on...
from pyfract.fraction import Fraction
f1 = Fraction(1, 2)
f2 = Fraction(1, 4)
print(f1 < f2) # False
print(f1 <= f2) # False
print(f1 > f2) # True
print(f1 >= f2) # True
print(f1 == f2) # False
print(f1 != f2) # True
print(f1 < 0.5) # False
print(f1 <= 0.5) # True
print(f1 > 0.5) # False
print(f1 >= 0.5) # True
print(f1 == 0.5) # True
print(f1 != 0.5) # False
Conversion from float
from pyfract.fraction import Fraction
print(Fraction.from_float(0.5)) # [1/2]
print(Fraction.from_float(0.134)) # [67/500]
Accurate conversion from float
Fast conversion from float doesn't work so well for float like 0.333333333333333333. This method is much slower, but allows to convert floats more accurately
from pyfract.fraction import Fraction
x = 1 / 3
print(x) # 0.3333333333333333
f = Fraction.from_float_accurately(x)
print(f) # [1/3]
accuracy defines up to how many decimal places the fraction must be accurate. By default it is 8
from pyfract.fraction import Fraction
f = Fraction.from_float_accurately(0.31987398749812214, accuracy=10)
print(f) # [44981/140621]
To float
from pyfract.fraction import Fraction
f = Fraction(1, 2)
print(f.to_float()) # 0.5
Contributing
Make a fork of this repository, clone codebase, create new branch, make your changes, commit it, push to remote, create pull request to this repository. Any contribution highly appreciated
Metadata
Release files for pyfract 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfract-1.4.1-py3-none-any.whl | Python 3 | none | any | Details for pyfract-1.4.1-py3-none-any.whl |
Release files / pyfract-1.4.1-py3-none-any.whl
| Download URL | pyfract-1.4.1-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e4a380c505fd4b5ac4ba65032a73b24bcd5974b35d8943f100212331115d96a
|
|
BLAKE2b-256 checksum How to use checksums |
ba083c6917a2ac1562789018c693542fb65c006498c66fcf12b6c0b9427210ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
|
Security for pyfract 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.