ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 10 Apr 2022 22:45:17 +0200Obtaining a RealNumber in Pythonhttps://ask.sagemath.org/question/56426/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.
<s>Note that to use `RealNumber` I need to import it explicitly (it's not included in `sage.all `).</s> <- 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?Tue, 30 Mar 2021 07:49:35 +0200https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/Answer by slelievre for <p>I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via <code>from sage.all import *</code>.</p>
<p>Most functions work fine, however when I try to obtain a <code>RealNumber</code> such as in the following code:</p>
<pre><code>from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)
</code></pre>
<p>I get this error:</p>
<pre><code>TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class
</code></pre>
<p>I've tried using strings as well and that didn't work either.</p>
<p><s>Note that to use <code>RealNumber</code> I need to import it explicitly (it's not included in <code>sage.all</code>).</s> <- This was an incorrect assumption on my part. After <a href="/users/1092/slelievre/">@slelievre</a>'s answer, I looked if <code>create_RealNumber</code> was imported by <code>sage.all</code> and it indeed is. Ultimately, to get <code>RealNumber</code> to work, only the <code>sage.all</code> import is needed.</p>
<h3>Question:</h3>
<p>How can I use <code>RealNumber</code> in my Python module?</p>
https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?answer=56428#post-id-56428One 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
Tue, 30 Mar 2021 11:48:03 +0200https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?answer=56428#post-id-56428Comment by Dan-K for <p>One nice way to figure out the correct import statements is
to use the <code>import_statements</code> function.</p>
<p>In this case:</p>
<pre><code>sage: import_statements('RealNumber')
from sage.rings.real_mpfr import create_RealNumber as RealNumber
</code></pre>
https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?comment=56429#post-id-56429Thank you so much!Tue, 30 Mar 2021 15:53:45 +0200https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?comment=56429#post-id-56429Answer by Dan-K for <p>I have a python module imported by a SageMath notbook and in this module, I import and use Sage functionality via <code>from sage.all import *</code>.</p>
<p>Most functions work fine, however when I try to obtain a <code>RealNumber</code> such as in the following code:</p>
<pre><code>from sage.rings.real_mpfr import RealNumber
# ...
test = RealNumber(1.2)
</code></pre>
<p>I get this error:</p>
<pre><code>TypeError: Cannot convert float to sage.rings.real_mpfr.RealField_class
</code></pre>
<p>I've tried using strings as well and that didn't work either.</p>
<p><s>Note that to use <code>RealNumber</code> I need to import it explicitly (it's not included in <code>sage.all</code>).</s> <- This was an incorrect assumption on my part. After <a href="/users/1092/slelievre/">@slelievre</a>'s answer, I looked if <code>create_RealNumber</code> was imported by <code>sage.all</code> and it indeed is. Ultimately, to get <code>RealNumber</code> to work, only the <code>sage.all</code> import is needed.</p>
<h3>Question:</h3>
<p>How can I use <code>RealNumber</code> in my Python module?</p>
https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?answer=61905#post-id-61905`RealNumber` can be imported and used from `sage.all`:
from sage.all import RealNumber
# ...
test = RealNumber(1.2)Sun, 10 Apr 2022 22:45:17 +0200https://ask.sagemath.org/question/56426/obtaining-a-realnumber-in-python/?answer=61905#post-id-61905