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.Float() Method:
We can convert integer values to floating-point values using the Float() method of the sympy module, and we can also set the precision values. Precision is set at 15 by default.
Syntax:
sympy.Float()
Return Value:
The floating-point number is returned by the Float() function.
sympy.Float() Method in Python
Method #1: Without Specifying the Precision Value
Here we do not give Precision Value. Hence it takes 15 by default.
Approach:
- Import all the functions from sympy module using the import keyword
- Pass some random Integer value to the Float() function to convert the given integer number to the floating-point number.
- Here it takes precision as 15 by default.
- Store it in a variable.
- Print the given Integer after converting it to a floating-point number.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass some random Integer value to the Float() function to convert the # given integer number to the floating-point number. # Here it takes precision as 15 by default. # Store it in a variable. float_num = Float(25) # Print the given Integer after converting it to a floating-point number. print("The given Integer after converting it to a floating-point number:") print(float_num)
Output:
The given Integer after converting it to a floating-point number: 25.0000000000000
Method #2: With Specifying the Precision Value
Approach:
- Import all the functions from sympy module using the import keyword
- Pass some random Integer, precision values as arguments to the Float() function to convert the given integer number to the floating-point number with the given precision
- Store it in a variable.
- Print the given Integer after converting it to a floating-point number with the given precision.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass some random Integer, precision values as arguments to the Float() function # to convert the given integer number to the floating-point number with the given precision # Store it in a variable. float_num = Float(25, 5) # Print the given Integer after converting it to a floating-point number with the given precision print("Converting given Integer to a floating-point number with the given precision:") print(float_num)
Output:
Converting given Integer to a floating-point number with the given precision: 25.000