Ask Your Question

# How to "recover" real number from expression?

How do I separate a RealNumber from an Expression, once it is constructed?

sage: b = RealField(200)(5)
sage: type(b)
<type 'sage.rings.real_mpfr.RealNumber'>
sage: type((b^x).operands()[0])
<type 'sage.symbolic.expression.Expression'>

edit retag close merge delete

## 2 Answers

Sort by ยป oldest newest most voted

You can use the pyobject method:

sage: b = RealField(200)(5)
sage: bb = (b^x).operands()[0]
sage: bb.pyobject(), type(bb.pyobject())
(5.0000000000000000000000000000000000000000000000000000000000, <type 'sage.rings.real_mpfr.RealNumber'>)

more

## Comments

That's much easier, thank you! I don't think pyobject is a good name for this method though.

( 2012-06-13 23:29:38 +0200 )edit

Yeah, the name make s more sense when you're familiar with the underlying pynac library. Can you think of a better name that we could use as an alias?

( 2012-06-14 03:04:13 +0200 )edit

By explicit conversion:

sage: expr = (0.5^x).operands()[0]
sage: expr
0.500000000000000
sage: type(expr)
<type 'sage.symbolic.expression.Expression'>


As a machine double precision floating point number:

sage: RDF(expr)
0.5


As a 200 bit software floating point number:

sage: RealField(200)(expr)
0.50000000000000000000000000000000000000000000000000000000000


As a interval arithmetic 200 bit floating point number:

sage: RealIntervalField(200)(expr)
0.50000000000000000000000000000000000000000000000000000000000000?


And so on...

more

## Comments

Thanks, but I was looking for a way to maintain the original precision. It seems that RealNumber(expr) works, but for whatever reason there is a minor loss in precision; RealNumber(RealField(200)(5)).prec() = 196. Any idea why?

( 2012-06-13 13:47:13 +0200 )edit

As well, is there a way to detect what type expr used to be? To detect that expr was a RealNumber, for example, or that (4^x).operands()[0] was an Integer.

( 2012-06-13 13:56:35 +0200 )edit

I found http://ask.sagemath.org/question/411/substituting-expressions-for-numbers. That looks like it could work for what I want to do. I'll post another answer if it does.

( 2012-06-13 13:59:52 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2012-06-13 00:55:45 +0200

Seen: 349 times

Last updated: Jun 13 '12