What is the number class in SymPy?
Last Updated :
16 Feb, 2022
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
from sympy import Float
print ( Float ( 5 ))
print ( Float ( 22.7 ))
print ( Float ( 22.7 , 4 ))
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
from sympy import Rational
print (Rational( 1 / 2 ))
print (Rational( "12/13" ))
print (Rational( 0.3 ))
print (Rational( 0.3 ).limit_denominator( 10 ))
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
print (Integer( 1.5 ))
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
print (S.NaN)
print (S.Infinity)
print (S.ImaginaryUnit)
print (S.Half)
|
Output
nan
oo
I
1/2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...