Hướng dẫn subtracting fractions python

Last update on August 19 2022 21:51:39 (UTC/GMT +8 hours)

Python Math: Exercise-46 with Solution

Write a Python program to add, subtract, multiply and divide two fractions.

Nội dung chính

  • Python Math: Exercise-46 with Solution
  • Obtaining a fraction[edit | edit source]
  • Operations[edit | edit source]
  • Unary Operations[edit | edit source]
  • Negation[edit | edit source]
  • Inversion[edit | edit source]
  • Addition[edit | edit source]
  • Subtraction[edit | edit source]
  • Multiplication[edit | edit source]
  • Division[edit | edit source]
  • Power[edit | edit source]
  • Algorithms[edit | edit source]
  • Farey reduction[edit | edit source]
  • Egyptian fractions[edit | edit source]
  • How do you solve fractions in Python?
  • Can you have fractions in Python?
  • How do you add fractions in programming?
  • How do you reduce a Fraction in Python?

Sample Solution:-

Python Code:

import fractions

f1 = fractions.Fraction(2, 3)
f2 = fractions.Fraction(3, 7)

print('{} + {} = {}'.format(f1, f2, f1 + f2))
print('{} - {} = {}'.format(f1, f2, f1 - f2))
print('{} * {} = {}'.format(f1, f2, f1 * f2))
print('{} / {} = {}'.format(f1, f2, f1 / f2))

Sample Output:

2/3 + 3/7 = 23/21                                                                                             
2/3 - 3/7 = 5/21                                                                                              
2/3 * 3/7 = 2/7                                                                                               
2/3 / 3/7 = 14/9

Flowchart:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create the fraction instances of decimal numbers.
Next: Write a Python program to convert a floating point number (PI) to an approximate rational value on the various denominator.

I am trying to add two fractions in python

if input 1/4 + 1/4, I am expecting 1/2 result

I built a fraction class with an __add__ method for addition

from fractions import gcd

class fraction:
    def __init__(self, numerator, denominator):
        self.num = numerator
        self.deno = denominator
    def __add__(self, other):
        self.sumOfn = self.num + other.num
        self.sumOfd = gcd(self.deno,other.deno)
        return(self.sumOfn, self.sumOfd)



print(fraction(1,4)+fraction(1,4))

However I am getting 2,4 as output, which is actually 1/2, just not simplified. How could I fix that problem ?

The official Python documentation for the fractions module is here.

Writing numbers that aren't integers as fractions dates back to the Egyptians and the Babylonians, before the use of decimals. Whenever the denominator is not a power of 10, we still often use fractions, although they may be somewhat hidden:

  1. If we say a man is 5 foot 7 inches, his height in feet is (there are twelve inches to the foot);
  2. When we say it's 8:13 am, we mean that exactly hours (493 minutes) have passed since midnight.
  3. When Romeo complains that he has waited more than three quarters of an hour for Juliet, he is expressing the duration of his anticipation as a fraction...
  4. Probabilities are often given as fractions (usually Egyptian fractions), as in "I have a one in ten million chance of winning the lottery", or "the odds are 5 to 1".
  5. Statistics may also be quoted as fractions: "5 out of every 6 people wear jumpers".

The equation 0.2+0.5=0.7 can be written

, but the equation can't be written exactly using decimal numbers. Python gives the result of 1/2+1/3 as 0.8333333333333333; despite all the digits, this is not the exact answer.

For exact calculations with fractions, Python has a fractions module. To use the scripts in this chapter, start with the line:

This will import everything from the fractions module.

Obtaining a fraction[edit | edit source]

To enter the fraction in Python, use Fraction(n,d), remembering to import it from the fractions module:

from fractions import *
a = Fraction(24,10)
print(a)             # 12/5

We find that Python automatically simplified the fraction when it was instantiated.

If you enter 0 as the denominator, the fraction won't be created, and an error message will occur. We can't divide a number by 0.

Once we have calculated a fraction, we can get its numerator and denominator:

from fractions import *
a = Fraction(24,10)
print(a.numerator)
print(a.denominator)

Of course, the numerator of is not 24, once it has been simplified.

To get the value of the fraction (its numerator divided by its denominator), we can convert it to a 'float' - this refers to how computers typically store numbers other than integers (as 'floating point' numbers):

from fractions import *
a = Fraction(24,10)
print(float(a))

We can also convert a real number to a fraction, but the result may be surprising, if the number is one that can't be represented exactly in binary:

from fractions import *
a = Fraction.from_float(1.2)
print(a)

Instead of , we get the fraction . This is because can't be stored precisely in binary, just as can't be written exactly as a decimal. In this case, we can get a better result by going straight from the decimal to the fraction, without the computer converting it to binary:

from fractions import *
a = Fraction('1.2')
print(a)

Operations[edit | edit source]

Operations on fractions are written as for other numbers, but the result is generally a fraction.

Unary Operations[edit | edit source]

Negation[edit | edit source]

To negate a fraction, precede it with the - sign. For example:

from fractions import *
a = Fraction(2,-3)
print(-a)

Inversion[edit | edit source]

To find the inverse of a fraction, divide 1 by it:

from fractions import *
a = Fraction(5,4)
print(1/a)

Addition[edit | edit source]

The sum of two fractions is a fraction:

from fractions import *
a = Fraction(34,21)
b = Fraction(21,13)
print(a+b)

Subtraction[edit | edit source]

The difference of two fractions is a fraction:

from fractions import *
a = Fraction(34,21)
b = Fraction(21,13)
print(a-b)

Multiplication[edit | edit source]

The product of two fractions is a fraction:

from fractions import *
a = Fraction(34,21)
b = Fraction(21,13)
print(a*b)

Division[edit | edit source]

The quotient of two fractions is a fraction (so long as the second is not zero):

from fractions import *
a = Fraction(34,21)
b = Fraction(21,13)
print(a/b)

The remainder (or modulo) can also be found for fractions. The result is a fraction:

from fractions import *
a = Fraction(32,7)
b = Fraction(7,2)
print(a%b)

Power[edit | edit source]

If the exponent is an integer, the power of a fraction is a fraction:

from fractions import *
a = Fraction(3,2)
print(a**12)
print(a**(-1))

But if the exponent is a real number, the power of a fraction is a real number:

from fractions import *
a = Fraction(9,4)
print(a**0.5)

Algorithms[edit | edit source]

Farey reduction[edit | edit source]

Creating a Farey reduction is easy with Python's fractions module:

from fractions import *
def Farey(a,b):
    n = a.numerator+b.numerator
    d = a.denominator+b.denominator
    return Fraction(n,d)


a = Fraction(3,4)
b = Fraction(1,13)
print(Farey(a,b))

This can be used make a Stern-Brocot tree; that may not clarify much, but it has mathematical uses.

Egyptian fractions[edit | edit source]

An Egyptian fraction is made of a series of inverse integers; the Egyptians had a reputation for not using numerators. Any fraction can be written as a sum of Egyptian fractions, and we can use Fibonacci's algorithm to find such a sum for any fraction. In the Python example below, the algorithm produces a list of fractions, all with numerator 1, which add up to the given fraction f. Since f may be greater than 1, we start the list with an integer:

from fractions import *
from math import ceil

def egypt(f):
    e = int(f)
    f -= e
    parts = [e]
    while(f.numerator>1):
        e = Fraction(1, int(ceil(1/f)))
        parts.append(e)
        f -= e
    parts.append(f)
    return parts

a = Fraction(21,13)
print(egypt(a))            # [1, Fraction(1, 2), Fraction(1, 9), Fraction(1, 234)]
print(sum(egypt(a))        # 21/13 (confirming that these fractions add up to the original value)

Some explanation of the code above: the denominator of each Egyptian fraction should be an integer, and greater than the inverse of the fraction f, so that the algorithm converges. One solution would be to round the inverse down to an integer (with int) and add 1. But if the inverse of f is an integer, adding 1 would lead to an infinite series. So we use the ceil function, to round up to an integer. Therefore:

  1. We had to import this function from the math module.
  2. The result of ceil(1/f) is a real number, not an integer, so we can't use it directly in a fraction (Python gives an error). We convert it into an integer using int.

Finally, we have to add the last Egyptian fraction to the list, to complete the series.

Running the script above, we find that .

How do you solve fractions in Python?

You have two options:.

Use float.as_integer_ratio() : >>> (0.25).as_integer_ratio() (1, 4) (as of Python 3.6, you can do the same with a decimal. Decimal() object.).

Use the fractions.Fraction() type: >>> from fractions import Fraction >>> Fraction(0.25) Fraction(1, 4).

Can you have fractions in Python?

Unlike int or float , fractions aren't a built-in data type in Python, which means you have to import a corresponding module from the standard library to use them.

How do you add fractions in programming?

Program to add two fractions.

#include .

int main().

int a, b,c,d,x,y,i,gcd;.

printf(“\nEnter the numerator for 1st number : “);.

scanf(“%d”,&a);.

printf(“\nEnter the denominator for 1st number : “);.

scanf(“%d”,&b);.

How do you reduce a Fraction in Python?

Reduce Fractions Function Python.

A function that reduces/simplifies fractions using the Euclidean Algorithm, in Python..

def reducefract(n, d):.

'''Reduces fractions. n is the numerator and d the denominator. '''.

def gcd(n, d):.

while d != ... .

t = d..