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 sympyfrom sympy import FloatÂ
# converting integer to floatprint(Float(5))Â
# limiting the digitsprint(Float(22.7))print(Float(22.7, 4))Â
# Scientific notation to number formatprint(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 sympyfrom sympy import RationalÂ
# representing a rational numberprint(Rational(1/2))Â
# Representing a string in p/q formprint(Rational("12/13"))Â
print(Rational(0.3))Â
# limiting the digits in denominatorprint(Rational(0.3).limit_denominator(10))Â
# Passing 2 numbers as arguments to # Rational classprint(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 integerprint(Integer(1.5))Â
# converting rational to integerprint(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 numberprint(S.NaN)Â
# represents Infinityprint(S.Infinity)Â
# represents imaginary valueprint(S.ImaginaryUnit)Â
# represents 1/2 valueprint(S.Half) |
Output
nan oo I 1/2
