Rational class

Rational class is used to represent the numbers in p/q form. i.e., numbers of the form numerator/denominator. Syntax of Rational class mentioned below

from sympy import Rational
Rational(any_Number)

Here any_Number may be a rational number, integer, or floating-point value.

Before going to use the Rational class first it needs to be imported. Rational class is capable of converting a string to a rational number and also limiting the denominator value if needed.

Python3




# import Rational class from sympy
from sympy import Rational
 
# representing a rational number
print(Rational(1/2))
 
# Representing a string in p/q form
print(Rational("12/13"))
 
print(Rational(0.3))
 
# limiting the digits in denominator
print(Rational(0.3).limit_denominator(10))
 
# Passing 2 numbers as arguments to
# Rational class
print(Rational(5, 7))


Output

1/2
12/13
5404319552844595/18014398509481984
3/10
5/7

What is the number class in SymPy?

Number class is represented atomic numbers in SymPy Python. It has three subclasses. They are Float, Rational, and Integer. These subclasses are used to represent different kinds of numbers like integers, floating decimals, and rational numbers. There are also some predefined singleton objects that represent NaN, Infinity, and imaginary numbers.

Similar Reads

Float class

Float class is used to represent the floating-point numbers. Syntax of this Float class is given below...

Rational class

...

Integer class

Rational class is used to represent the numbers in p/q form. i.e., numbers of the form numerator/denominator. Syntax of Rational class mentioned below...