Numeric Types

Sympy library has the numeric types Rational and Real. The Rational class represents a rational number as a pair of two integers: the numerator and the denominator, so Rational(1,4) represents 1/4, Rational(5,2) represents 5/2:
>>> a = sympy.Rational(1,10)
>>> a
1/10
>>> b = sympy.Rational(45,46)
>>> b
45/46
>>> a*b
9/92
>>> a-b
-101/115
>>> a+b
124/115
Rational class works with rational expressions exactly. This is in contrast to Python’s standard float data type which uses floating point representation to approximate numbers. We can convert the sympy.Rational type into a Python floating point variable using float or the evalf method of the Rational object. The evalf method can take an argument that specifies how many digits should be computed for the floating point approximation.
>>> c = sympy.Rational(5,6)
>>> c
5/6
>>> float(c)
0.8333333333333334
>>> c.evalf()
0.833333333333333
>>> c.evalf(20)
0.83333333333333333333

1 komentar:

  1. Wynn Las Vegas casino review - Dr.MCD
    The 정읍 출장안마 Wynn casino is under the Las Vegas Police 태백 출장마사지 Department 순천 출장마사지 and as such, it is one of the best in the 안양 출장안마 country and is considered one of the best 보령 출장마사지 gambling

    OdgovoriIzbriši