Convert Rational to Float

175 views
Skip to first unread message

sandona...@gmail.com

unread,
Aug 6, 2020, 5:59:59 PM8/6/20
to sympy
Hello,

consider this trivial example:

x = symbols("x")
expr = S(10) / 3 * exp(S(5) / 2) * x**(S(5) / 2)

I would like to convert all rational numbers to float. I though it would be easy, for example:

expr.replace(lambda e: e.is_Rational, lambda e: e.evalf())

However, Rational numbers in the exponents are not converted. Why?

Thanks in advance!

Oscar Benjamin

unread,
Aug 6, 2020, 7:28:15 PM8/6/20
to sympy
I'm not sure why that doesn't work but you can just use evalf directly:

In [7]: expr
Out[7]:
5/2 5/2
10⋅x ⋅ℯ
────────────
3

In [8]: expr.n()
Out[8]:
2.5
40.6083132023449⋅x

Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6df11339-8e3a-4e12-bbde-ec893a2187cdn%40googlegroups.com.

Aaron Meurer

unread,
Aug 7, 2020, 3:01:12 AM8/7/20
to sympy
Reply all
Reply to author
Forward
0 new messages