# Convert symbolic expressions like sqrt(2) or exp(1) to rational numbers [closed]

I have coefficents of a rational polynomial f(x) in terms of symbolic expressions like sqrt(2) and exp(1).

How can I convert these coefficients to rational number approximations of them, so that I can work in a structure like a polynomial ring?

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by jjack close date 2014-07-26 12:57:17.227063

Sort by » oldest newest most voted Maybe something like that:

sage: R.<x> = SR['X']
sage: ex = sqrt(2) + exp(1)*x
sage: realring = RR['x']
sage: ratring = QQ['x']
sage: ratring(realring(ex))
268876667/98914198*x + 131836323/93222358

more

That sounds good. Is there an advantage in the additional step to working over a field of real numbers? What if I wanted to invert a power series with those coefficients, are rational coeffients better than real numbers?

Well, you can use either reals or rationals, they will play exactly the same role in any computation with polynomials. If you want rationals, you first need to convert to reals, so that one can then approximate them to rationals. By the way, could you please upvote and accept my answer if it suits you ?

I'll maybe post a separate question for the precision issue. Thank you for your answer.