Ask Your Question
1

Obtaining a RealNumber in Python

asked 2021-03-30 07:49:35 +0200

Dan-K gravatar image

updated 2021-03-30 16:18:12 +0200

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 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?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-04-10 22:45:17 +0200

Dan-K gravatar image

updated 2022-04-10 22:49:54 +0200

RealNumber can be imported and used from sage.all:

from sage.all import RealNumber
# ...
test = RealNumber(1.2)
edit flag offensive delete link more
3

answered 2021-03-30 11:48:03 +0200

slelievre gravatar image

One nice way to figure out the correct import statements is to use the import_statements function.

In this case:

sage: import_statements('RealNumber')
from sage.rings.real_mpfr import create_RealNumber as RealNumber
edit flag offensive delete link more

Comments

Thank you so much!

Dan-K gravatar imageDan-K ( 2021-03-30 15:53:45 +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

1 follower

Stats

Asked: 2021-03-30 07:49:35 +0200

Seen: 298 times

Last updated: Apr 10 '22