# Rational numbers. Class Fraction

### Contents

Search other websites:

##### 1. The concept of a rational number. Representation of rational numbers. Class Fraction

A rational number is a number that can be represented as a rational fraction m/n, where m, n, respectively, are the numerator and denominator, which have an integer value. For example, in fraction 5/6, the value m = 5, the value n = 6.

The Python programming language for working with rational numbers offered a Fraction class. In the class, the numerator m and the denominator n are respectively implemented. In Fraction class is automatically performed the simplification fraction (e.g., 9/18 => 1/2).

To use the capabilities of the Fraction class, you must first include the fractions module

`from fractions import Fraction`

### ⇑

##### 2. Creating an object of the Fraction class

An object of the Fraction class can be created in one of two ways.

Method 1. Using a constructor that contains integer values for the numerator and denominator.

Example.

```a = Fraction(5, 6) # a = 5/6 - rational number
b = Fraction(8, 12) # b = 2/3 - rational number```

In the above example, in a line

`b = Fraction(8, 12)`

the value of the numerator 8 and the denominator 12 of the variable b will be automatically simplified to 2/3. That is, the numerator in the class is 2, the denominator is 3.

Method 2. Using a constructor that gets a string with a real value.

Example.

```a = Fraction('1.33') # a = 133/100
b = Fraction('3.719') # b = 3719/1000
c = Fraction('-1.5') # c = -3/2
d = Fraction('3.7') + Fraction('5.2') # d = 89/10```

### ⇑

##### 3. Operations on rational numbers. Examples

The following arithmetic operations can be performed on objects of the Fraction class:

• substraction ();
• multiplication (*);
• division (/);
• taking the remainder of the division (%).

Examples.

```# Rational numbers
from fractions import Fraction

a = Fraction(5, 6) + Fraction(3, 2) # a =   7/3
b = a - Fraction(3, 5) # b = 26/15
c = b * Fraction(101, 202) # c = 13/15
d = c / b # d = 1/2
e = d % a # e = 1/2```

Operation exponentiation of Fraction type number returns the real result

`f = Fraction(1,2)**Fraction(1,2) # f = 0.7071067811865476`

### ⇑

##### 4. The advantages of using rational numbers. Example

As you know, operations with real numbers have a precision limit, which depends on the capabilities of hardware that implement the mathematics of real numbers. Compared to real numbers, rational numbers provide

• the desired precision of calculations;
• automatic simplification of the result.

Example. The example demonstrates the loss of precision for the expression 0.2 + 0.2 + 0.2-0.4.

```# Rational numbers, advantages of the use
from fractions import Fraction

a = 0.2+0.2+0.2-0.4 # a =   0.20000000000000007 -the precision is lost
b = Fraction('0.2')+Fraction('0.2')+Fraction('0.2')-Fraction('0.4') # b = 1/5
print('a = ', a)
print('b = ', b)```

The result of the program

```a = 0.20000000000000007
b = 1/5```

### ⇑

##### 5. Function as_integer_ratio(). Example

The as_integer_ratio() function returns the numerator and denominator that matches the given number. The general form of the method call

`(real_number).as_integer_ratio()`

here

• real_number – floating point number.

The function is used to support conversion to rational numbers.

Example.

```# function as_integer_ratio()
from fractions import Fraction

# conversion to the fractional number
a = (3.5).as_integer_ratio() # a = (7, 2)
b = (11.7).as_integer_ratio() # b = (3293257227514675, 281474976710656)

# conversion to type Fraction
a = 8.5
c = Fraction(*a.as_integer_ratio()) # c =   17/2```

In the above example, the symbol * means the syntax for unpacking a tuple into separate arguments.

### ⇑

##### 6. Function Fraction.from_float(). Example

The from_float() method of the Fraction class allows you to get the numerator and denominator of a real number as well as the as_integer_ratio() method.

Example.

```# function from_float of the Fraction class
from fractions import Fraction

y = Fraction.from_float(2.25) # y = 9/4

x = 3.8 # тип float
y = Fraction.from_float(x) # y = 4278419646001971/1125899906842624

x = -8.75
y = Fraction.from_float(x) # y = -35/4```

### ⇑

##### 7. Function float(). Example

The from_float() function of the Fraction class allows you to get the numerator and denominator of a real number as well as the as_integer_ratio() method.

Example.

```# function float() of class Fraction
from fractions import Fraction

# argument is a variable of real type
x = Fraction(11, 4)
y = float(x) # y = 2.75

# argument - number
y = float(Fraction(7,6)) # y =   1.1666666666666667```