Skip to main content

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfract-1.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pyfract-1.3-py3-none-any.whl.

File metadata

  • Download URL: pyfract-1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.9

File hashes

Hashes for pyfract-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 efb19d741ca3f4585a170b0d9435c938ae571ac2374c6df37cf545c8a91bc43c
MD5 b7f346f2ca690142a44f2799c3345346
BLAKE2b-256 bfed78a0ff45ac3215be796160a1e3cb1bc0a5b9217a4cec69b360db788c69bd

See more details on using hashes here.

Supported by

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