Ask Your Question

How to "recover" real number from expression?

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

Eviatar Bach gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-06-13 23:11:32 +0200

Mike Hansen gravatar image

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'>)
edit flag offensive delete link more


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

Eviatar Bach gravatar imageEviatar Bach ( 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?

Mike Hansen gravatar imageMike Hansen ( 2012-06-14 03:04:13 +0200 )edit

answered 2012-06-13 07:12:50 +0200

Volker Braun gravatar image

By explicit conversion:

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

As a machine double precision floating point number:

sage: RDF(expr)

As a 200 bit software floating point number:

sage: RealField(200)(expr)

As a interval arithmetic 200 bit floating point number:

sage: RealIntervalField(200)(expr)

And so on...

edit flag offensive delete link more


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?

Eviatar Bach gravatar imageEviatar Bach ( 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`.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-13 13:56:35 +0200 )edit

I found That looks like it could work for what I want to do. I'll post another answer if it does.

Eviatar Bach gravatar imageEviatar Bach ( 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

Question Tools


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

Seen: 309 times

Last updated: Jun 13 '12