Python fractions With Examples

Python fractions module

As we know, a fraction is a number which represents a whole number being divided into multiple parts. Python fractions module allows us to manage fractions in our Python programs.

Managing fractions

In this Python post, we will manage fractions and perform various operations on them. Let’s get started.

Creating Fractions

Fraction class in Python allows us to make its instance in various ways. Here is a sample program:

Let’s see the output for this program:
Python fractions
This means that we can easily convert a fraction into a String and manage it to debug in our prograns. Also, notice that in the last fraction 30/4, it was automatically resolved to the lowest form as 15/2.

We can also create a fraction from its String representation actually, like:

Converting Decimals to Fractions

It is also possible to convert a decimal into a Fractional number. Let’s look at a code snippet:

Let’s see the output for this program:
python-decimal-to-fractions
Pretty easy to manage, right? But here is a catch, decimal values which cannot be exactly turned into a fraction might yield unexpected results like:

This will give an output as:
python-decimal-to-fractions-incorrect
Noticed the issue with 0.1 representation? Let’s understand why this happens.

Issue with 0.1 representation

As we clearly know, floats consist of two parts, an integer and an exponent part of which the base is taken to and multiplied by the integer part.

Base 10 makes working with math very easy as with Base 10, every number can be presented very easily. 0.5 can be represented as 5 x 10?¹. So, if we add numbers like 0.5 and 0.2, the result will be 0.7. But, the computers don’t work that way. Computers use Base 2 and not Base 10.

The issue arises with numbers which can be represented by Base 10 but not Base 2. Those numbers have to be rounded off to their closest equivalent. Considering the IEEE 64-bit floating point format, the closest number to 0.1 is 3602879701896397 x 2???, and the closest number to 0.2 is 7205759403792794 x 2???; adding them gives 10808639105689191 x 2???, or an exact decimal value of 0.3000000000000000444089209850062616169452667236328125. Floating point numbers are generally rounded for display.

Arithmetic Operations

We can also perform Arithmetic Operations quite easily with fractional numbers. Let’s see some examples here.

Performing Mathematical operations

Let’s construct a simple example that shows how to perform arithmetic operations with fractional numbers. Here is a sample program:

Let’s see the output for this program:

python-fraction-arithmetic

Getting parts of fractions

It is possible to get only the numerator or the denominator of a fraction. Let’s look at a code snippet on how this can be done:

Let’s see the output for this program:

python-fraction-parts

Making Approximations

We can use the fractions module to approximate and round off a number to a rational value. Here is a sample program:

Let’s see the output for this program:

python-denominator-limit

The limit_denominator() function finds and returns the closest fraction that has the denominator with maximum value of num passed to it.

Rounding off fractions

It is possible to round off fractions by the number of digits we want in the denominator. Let’s look at a code snippet:

Let’s see the output for this program:
python-fraction-round
Note that round() is a default Python’s interpreter function and doesn’t want any imports.

Mixing Math with Fractions

In a final example, we will bring some functions from math library and mix them with fractional representations here. Like flooring a fraction etc. Let’s look at a code snippet:

Let’s see the output for this program:
python-fraction-with-math
The floor() function just rounds of a decimal equivalent and provides the nearest integer.

Conclusion

In this lesson, we studied how we can manage and use Fraction values in our Python program effectively.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: