Programming Language: Python

Namespace/Package Name: diofant

Class/Type: Rational

Examples at hotexamples.com: 34

Python Diofant Rational is a module that helps in dealing with rational numbers. It is a part of the Diofant library. It provides a class for representing rational numbers in Python. Rational numbers are represented as two integers, numerator and denominator, separated by a slash. The Rational class provides methods for performing arithmetic operations on rational numbers like addition, subtraction, multiplication, and division.

Code Example:

Code Example:

from sympy import Rational r1 = Rational(3, 4) r2 = Rational(5, 6) # Addition r3 = r1 + r2 print(r3) # 19/12 # Subtraction r4 = r1 - r2 print(r4) # -1/12 # Multiplication r5 = r1 * r2 print(r5) # 5/8 # Division r6 = r1 / r2 print(r6) # 9/10In the above code, we have created two Rational objects r1 and r2 with numerator and denominator as 3/4 and 5/6 respectively. We then perform arithmetic operations like addition, subtraction, multiplication, and division on these objects and store them in new Rational objects r3, r4, r5, and r6. Finally, we print out the values of these objects. The package library for Python Diofant Rational is Diofant.

Frequently Used Methods