Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Obtaining a RealNumber in Python

I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via from sage.all import *.

Most functions work fine, however when I try to obtain a RealNumber such as in the following code:

from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)

I get this error:

TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class

I've tried using strings as well and that didn't work either. Note that to use RealNumber I need to import it explicitly (it's not included in sage.all).

Question:

How can I use RealNumber in my Python module?

Obtaining a RealNumber in Python

I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via from sage.all import *.

Most functions work fine, however when I try to obtain a RealNumber such as in the following code:

from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)

I get this error:

TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class

I've tried using strings as well and that didn't work either. either.

Note that to use RealNumber I need to import it explicitly (it's not included in sage.all). <- This was an incorrect assumption on my part. After @slelievre's answer, I looked if it was imported by sage.all and it indeed is. Ultimately, to get RealNumber to work, only the sage.all import is needed.

Question:

How can I use RealNumber in my Python module?

Obtaining a RealNumber in Python

I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via from sage.all import *.

Most functions work fine, however when I try to obtain a RealNumber such as in the following code:

from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)

I get this error:

TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class

I've tried using strings as well and that didn't work either.

Note that to use RealNumber I need to import it explicitly (it's not included in sage.all). <- This was an incorrect assumption on my part. After @slelievre's answer, I looked if it create_RealNumber was imported by sage.all and it indeed is. Ultimately, to get RealNumber to work, only the sage.all import is needed.

Question:

How can I use RealNumber in my Python module?