Giter VIP home page Giter VIP logo

ezfraction's Introduction

ezFraction

Downloads total Downloads monthly PyPi Package Version PyPi status Supported python versions Github issues MIT License

ezFraction is a custom Python module for working with fractions.

Installation

From PyPI:

pip install ezFraction

From source:

git clone https://github.com/GooDeeJaY/ezFraction.git
cd ezFraction
python -m pip install .

Usage

Importing and initialization:

from ezFraction import Fraction

# passing string
Fraction("3.14")        # 157/50

# passing float
Fraction(1.24)          # 31/25

# passing int
Fraction(567)           # 567/1

# passing fraction
Fraction(28, 3)         # 28/3

Properties:

frac = Fraction(4.6)

# Properties
print(frac)             # 23/5
print(frac.decimal)     # 4.6
print(frac.numerator)   # 23
print(frac.denominator) # 5
print(frac.tuple)       # (23, 5)

By default fractions will be rounded to 5 decimal places, if you want more or less precision you can set different rounding or even switch off the rounding:

print(Fraction(3.14))                   # 333333/100000
print(Fraction(3.14, rounding=None))    # 4166666666666667/1250000000000000

When initializing Fraction, we can set reduce to False in order to get unreduced fraction:

print(Fraction(18.5))                   # 37/2
print(Fraction(18.5, reduce=False))     # 185/10

Also, there are .reduce() and .enlarge() methods that perform actions that their name tells. Both methods have new_obj parameter which is used to create new Fraction instances rather than applying methods to themselves. Apart from .reduce(), .enlarge() accepts one additional argument multiplier that is used to set the different multiplier for enlargement rather than 2 (default).

frac = Fraction(20.5) 
print(frac)                         # 41/2
print(frac.enlarge())               # 82/4
print(frac.enlarge(multiplier=3))   # 246/12
frac2 = frac.reduce(new_obj=True)
print(frac2, frac)                  # 41/2 246/12

You can use .with_denominator() method in order to set custom denominator:

frac = Fraction(0.32)                               # 8/25
custom = frac.with_denominator(35, to_str=True)     # 11.2/35

Since this creates an invalid fraction, conversion will not be saved in Fraction instances but rather will be returned as a tuple (default) or string. Also, there is a .with_numerator() method that does the same thing but with a numerator.

These methods can be useful when comparing fractions with others. For example, we have fractions 8/25 and 9/35, telling which one is greater by glance is not so easy. So here comes the aid of .with_denominator() method by which we convert 8/25 to 11.2/35. Now it is easy to tell that 11.2/35 is greater than 9/35

Last but not least, you can perform arithmetic operations with Fraction objects:

frac1 = Fraction(0.48)      # 12/25
frac2 = Fraction(1.52)      # 38/25

print(frac1 + frac2)        # 2/1
print(frac1 - frac2)        # -26/25
print(frac1 * frac2)        # 456/625
print(frac1 / frac2)        # 6/19

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.