Josh
Josh

Reputation: 151

How to change the type of a float number to Real Literal?

I am working with floats in SageMath and I would like to change the type of a float number from sage.rings.real_double.RealDoubleElement to sage.rings.real_mpfr.RealLiteral. I can switch the other way around.

print(type(0.1))
#output: <class 'sage.rings.real_mpfr.RealLiteral'>

print(type(RDF(0.1)))
#output: <class 'sage.rings.real_double.RealDoubleElement'>

I have tried a few methods but I could not find a way, and I was not able to find a way by looking at the documentations. Using RR(RDF(0.1)) does not quite work either as its type is <class 'sage.rings.real_mpfr.RealNumber'>.

Is it possible to change a float to sage.rings.real_mpfr.RealLiteral in SageMath?

Upvotes: 2

Views: 386

Answers (2)

John Palmieri
John Palmieri

Reputation: 1696

If you evaluate 1.3 in Sage, you get a RealLiteral:

sage: type(1.3)
<class 'sage.rings.real_mpfr.RealLiteral'>

You can run preparse(1.3) to find out how this happens:

sage: preparse('1.3')
"RealNumber('1.3')"

So in general you should use RealNumber:

sage: type(RealNumber(float(1.3)))
<class 'sage.rings.real_mpfr.RealLiteral'>

Finally, if you want to do this in a Python file, here's what to import:

sage: import_statements('RealNumber')
from sage.rings.real_mpfr import create_RealNumber as RealNumber

Upvotes: 0

Alexander Verner
Alexander Verner

Reputation: 71

You can convert it into a rational number like this: Rational(1.0). Then you can use it with symbolic algebra fields like QQbar.

Upvotes: 0

Related Questions