Fractions implementation for Python
Project description
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
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
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfract-1.4-py3-none-any.whl.
File metadata
- Download URL: pyfract-1.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ece0778cad8560ca1a588d00d840b1bcc60dcb1ff77abf8829ef648d4dcaa3c7
|
|
| MD5 |
c5833f50c22181f44e6a91f098b6d436
|
|
| BLAKE2b-256 |
31cda960a07cb7c5f2263d2f80f86015dc573b8dfd29dee33403e1ebe0e59c81
|