Open In App

What is the number class in SymPy?

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Float class

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

from sympy import Float
Float(any_Number)

Here any_Number is the floating-point number or any integer.

To use Float() one must import Float class from sympy package first. The float method was able to represent an Integer into floating-point numbers and can able to limit the digits after the decimal point. It is also capable to represent scientific notations into numbers.

Python3




# import Float class from sympy
from sympy import Float
 
# converting integer to float
print(Float(5))
 
# limiting the digits
print(Float(22.7))
print(Float(22.7, 4))
 
# Scientific notation to number format
print(Float('99.99E1'))


Output

5.00000000000000
22.7000000000000
22.70
999.900000000000

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

Integer class

The integer class in sympy is used to represent the integers. It converts floating-point numbers and rational numbers to integers. Syntax of Integer class is given below –

from sympy import Integer
Integer(any_Number)

Here any_Number may be an Integer, rational number, and floating-point number.

Python3




from sympy import Integer
 
# converting float to integer
print(Integer(1.5))
 
# converting rational to integer
print(Integer(500/200))


Output

1
2

Let’s look into an example code that describes some predefined singleton objects that represent a few important notations.

Python3




from sympy import S
 
# represents not a number
print(S.NaN)
 
# represents Infinity
print(S.Infinity)
 
# represents imaginary value
print(S.ImaginaryUnit)
 
# represents 1/2 value
print(S.Half)


Output

nan
oo
I
1/2


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads