Python sympy.Rational() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.Rational() Method:

We can obtain the rational form of any float value passed as an argument to Rational() function of the sympy module.

Syntax:

 sympy.Rational(value)

Return Value:

The rational form of the given float value is returned by the Rational() function.

sympy.Rational() Method in Python

Method #1: Using Rational() Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number(as float) as static input and store it in a variable.
  • Pass the above given float number as an argument to the Rational() function to get the Rational value for the given float number.
  • Store it in another variable.
  • Print the Rational value for the given float number.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number(as float) as static input and store it in a variable.
gvn_num = 0.5

# Pass the above given float number as an argument to the Rational() function to get the 
# Rational value for the given float number.
# Store it in another variable.
rslt = Rational(gvn_num)

# Print the Rational value for the given float number.
print("The Rational value for the given float number{",gvn_num,"} = ", rslt)

Output:

The Rational value for the given float number{ 0.5 } = 1/2

Method #2: Using Rational() Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number(as float) as user input using the input() function and store it in a variable.
  • Pass the above given float number as an argument to the Rational() function to get the Rational value for the given float number.
  • Store it in another variable.
  • Print the Rational value for the given float number.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number(as float) as user input using the input() function 
# and store it in a variable.
gvn_num = input("Enter some random float number = ")

# Pass the above given float number as an argument to the Rational() function to get the 
# Rational value for the given float number.
# Store it in another variable.
rslt = Rational(gvn_num)

# Print the Rational value for the given float number.
print("The Rational value for the given float number{",gvn_num,"} = ", rslt)

Output:

Enter some random float number = 1.2
The Rational value for the given float number{ 1.2 } = 6/5