0

Request you to suggest regarding the below problem.

First we have to divide a numerator by the denominator in order to obtain a floating point number. Then the digits after the decimal needs to be displayed as a fraction.

When 9 is divided by 4 it has to be displayed as 2 1/4.This is working fine.

When 13 is divided by 5 it needs to be displayed as 2 3/5.

My code is not working for the second case. Getting output as

2 5404319552844595/9007199254740992.

STEP1: Divided the numerator by the denominator to find the quotient Calculated the remainder using (nmr%dnr).

STEP 2: Divided the remainder obtained the in above step by the denominator again. Converted the floating point number into fractions by using the Fractions built in method.

STEP 3:
The output is stored in a tuple and the output is printed. t1=(int(q),r).

Code:

from fractions import Fraction

def printValue(num,den):
    rem=0.0;q=0.0;

    q=float(num)/float(den)
    rem=float(num)%float(den)

    if rem>0:
        #print ("%0.2f"%(rem/den))
        r=Fraction(round(rem/den,2))
        t1=(int(q),r)                    #Output is stored in a tuple
        return t1
    else:
        t2=(int(q),)
        return t2


num=int(input())
den=int(input())
z=printValue(num,den)
for i in z:
    print(i,end=" ")
martineau
  • 119,623
  • 25
  • 170
  • 301
Pranav
  • 11
  • 3
  • `9/4` can be represented precisely by a `float`. `13/5` can not. try keeping `num` and `den` as integers and to the calculations form there... – hiro protagonist Feb 28 '20 at 08:37
  • *First we have to divide a numerator by the denominator in order to obtain a floating point number*. Are you really sure of that requirement??? IMHO passing by a **floating point** number just makes the operation more complex... – Serge Ballesta Feb 28 '20 at 08:44

2 Answers2

3

You can use divmod.

def printValue(num, den):
    x, r = divmod(num, den)
    print(f'{x if x else ""} {r}/{den}')

printValue(9, 4)
# prints:
2 1/4

printValue(13, 5)
# prints:
2 3/5

printValue(2, 5)
# prints:
 2/5
James
  • 32,991
  • 4
  • 47
  • 70
0

The problem is that floating point is broken... or more exactly it has a limited accuracy. A fractional number can only be exactly represented if its denominator is a power of 2, which explains that 9/4 gives the exact and expected result.

But Python is great. The Fraction class has a limit_denominator() method which helps in finding a reasonable fractional approximation of a floating point by searching a limited denominator (by default 1000000).

Demo:

>>> f = 3/5
>>> Fraction(f)
Fraction(5404319552844595, 9007199254740992)
>>> Fraction(f).limit_denominator()
Fraction(3, 5)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252