0

I am trying to define a class Fraction:

 def __init__(self,num=0,denom=1):
        self.num = num
        self.denom = denom

And I also want to define a add method,

def __add__(self,right):

    if type(right) is int:
        return (self.num/self.denom) + right
    if type(right) is Fraction:
        return ((self.num/self.denom)+(right.num/right.denom))


def __radd__(self,left):
    return (self.num/self.denom)+left

It works, and always returns a float. However, I want it to return a fraction. For example: if I test:

f = Frac(1,2)
f + Frac(1,3) 
f + 2 
Frac(1,3) + f
2 + f

I always get:

*Error: f+Frac(1,3) -> 0.8333333333333333 but should -> 5/6
*Error: f+2 -> 2.5 but should -> 5/2
*Error: Frac(1,3)+f -> 0.8333333333333333 but should -> 5/6
*Error: 2+f -> 2.5 but should -> 5/2

Are there any methods that can convert the result from a float to a fraction? Thank you so much.

Jign
  • 149
  • 1
  • 1
  • 10
  • 3
    Is it what you are looking for? http://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions – Kirill Apr 22 '15 at 17:32
  • Is your intention that the `__add__` and `__radd__` special methods return instances of your `Frac` type? Because right now they don't. – Daniel Pryden Apr 22 '15 at 17:33
  • 2
    Are you aware that Python has a [`fractions` module](https://docs.python.org/2/library/fractions.html)? – Fred Larson Apr 22 '15 at 17:34
  • I know, but I just want to define a new class called Fraction. – Jign Apr 22 '15 at 17:35
  • 1
    Ok. There's nothing wrong with reinventing the wheel if the point is to learn how to make wheels. – Fred Larson Apr 22 '15 at 17:37
  • ...Well I just want to do some exercise about building a class. Thanks for your attention anyway – Jign Apr 22 '15 at 17:41

3 Answers3

4

Don't create a float in the first place. For example,

def __add__(self,right):

    if type(right) is int:
        return Fraction(self.num + right*self.denom, self.denom)
    elif if type(right) is Fraction:
        # Reducing the answer to lowest terms is left
        # as an exercise for the reader
        return Fraction(self.num*right.denom + right.num*self.denom,
                        self.denom*right.denom)
chepner
  • 497,756
  • 71
  • 530
  • 681
0

The Python documentation section on Emulating numeric types explains how to implement your own numeric type.

The numbers module in the standard library also contains an abstract base class for rational types (like your fraction type).

And of course there is also an existing implementation in the standard library that does exactly what you intend: fractions.Fraction. You can read its source code in Lib/fractions.py if you're interested in learning how it works.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
0

The fraction module of Python may help you:

https://docs.python.org/2/library/fractions.html

Example:

from fractions import Fraction
Fraction('3.1415926535897932').limit_denominator(1000)

Solution: Fraction(355, 113)

chuseuiti
  • 783
  • 1
  • 9
  • 32