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.Fri, 12 Jun 2015 23:43:31 +0200Conversion between RealFieldhttps://ask.sagemath.org/question/27093/conversion-between-realfield/ I found the following behaviour very unintuitive :
R128=RealField(128)
pi_r128=R128.pi()
print pi_r128*2
print pi_r128*2.
print RR(pi)*2
The corresponding output is :
6.2831853071795864769252867665590057684
6.28318530717959
6.28318530717959
The first line was the expected behavior.
The second line was not expected because floating number **2.** has the default precision contrary to **pi_r128** whose is precision is wider. As the 3rd line shows, the 128 bit precision is completely ignored from **pi_r128*2** calculation.
So I was wondering how conversion between RealField numbers was done.
Fri, 12 Jun 2015 17:33:51 +0200https://ask.sagemath.org/question/27093/conversion-between-realfield/Answer by tmonteil for <p>I found the following behaviour very unintuitive :</p>
<pre><code>R128=RealField(128)
pi_r128=R128.pi()
print pi_r128*2
print pi_r128*2.
print RR(pi)*2
</code></pre>
<p>The corresponding output is :</p>
<pre><code>6.2831853071795864769252867665590057684
6.28318530717959
6.28318530717959
</code></pre>
<p>The first line was the expected behavior. </p>
<p>The second line was not expected because floating number <strong>2.</strong> has the default precision contrary to <strong>pi_r128</strong> whose is precision is wider. As the 3rd line shows, the 128 bit precision is completely ignored from <strong>pi_r128*2</strong> calculation.</p>
<p>So I was wondering how conversion between RealField numbers was done.</p>
https://ask.sagemath.org/question/27093/conversion-between-realfield/?answer=27095#post-id-27095You should have a look at the documentation about coercion, for example, see http://doc.sagemath.org/html/en/tutorial/tour_coercion.html and http://doc.sagemath.org/html/en/reference/coercion/index.html
When you multiply two elements that do not have the same parent, the coercion model first put both elements in a common parent and do the multiplication within this parent.
In your first case, you multiply an element of `RealField(128)` with an element of `ZZ` (which is exact), the common parent is `RealField(128)` and the multiplication is done there.
In your second case, you multiply an element of `RealField(128)` with an element of `RR=RealField(53)`, the common parent is `RealField(53)` since you can not increase the precision of a number. Imagine the extreme case `RealField(2)(pi) = 3.0` to be multiplied with `RR(pi) = 3.14159265358979` would you expect Sage to silently give you as result `RR(pi)*RR(3.0) = 9.42477796076938` while `RR(pi)^2 = 9.86960440108936` ?
Fri, 12 Jun 2015 18:04:18 +0200https://ask.sagemath.org/question/27093/conversion-between-realfield/?answer=27095#post-id-27095Comment by candide for <p>You should have a look at the documentation about coercion, for example, see <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">http://doc.sagemath.org/html/en/tutor...</a> and <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html">http://doc.sagemath.org/html/en/refer...</a></p>
<p>When you multiply two elements that do not have the same parent, the coercion model first put both elements in a common parent and do the multiplication within this parent.</p>
<p>In your first case, you multiply an element of <code>RealField(128)</code> with an element of <code>ZZ</code> (which is exact), the common parent is <code>RealField(128)</code> and the multiplication is done there.</p>
<p>In your second case, you multiply an element of <code>RealField(128)</code> with an element of <code>RR=RealField(53)</code>, the common parent is <code>RealField(53)</code> since you can not increase the precision of a number. Imagine the extreme case <code>RealField(2)(pi) = 3.0</code> to be multiplied with <code>RR(pi) = 3.14159265358979</code> would you expect Sage to silently give you as result <code>RR(pi)*RR(3.0) = 9.42477796076938</code> while <code>RR(pi)^2 = 9.86960440108936</code> ?</p>
https://ask.sagemath.org/question/27093/conversion-between-realfield/?comment=27096#post-id-27096Thanks for your answer and the link to the reference manual (BTW, the two links points to the same url). So if I understand correctly, the statements cm = sage.structure.element.get_coercion_model();cm.explain(R128,ZZ);cm.explain(R128,RR); give the response to my problem.Fri, 12 Jun 2015 19:09:20 +0200https://ask.sagemath.org/question/27093/conversion-between-realfield/?comment=27096#post-id-27096Comment by tmonteil for <p>You should have a look at the documentation about coercion, for example, see <a href="http://doc.sagemath.org/html/en/tutorial/tour_coercion.html">http://doc.sagemath.org/html/en/tutor...</a> and <a href="http://doc.sagemath.org/html/en/reference/coercion/index.html">http://doc.sagemath.org/html/en/refer...</a></p>
<p>When you multiply two elements that do not have the same parent, the coercion model first put both elements in a common parent and do the multiplication within this parent.</p>
<p>In your first case, you multiply an element of <code>RealField(128)</code> with an element of <code>ZZ</code> (which is exact), the common parent is <code>RealField(128)</code> and the multiplication is done there.</p>
<p>In your second case, you multiply an element of <code>RealField(128)</code> with an element of <code>RR=RealField(53)</code>, the common parent is <code>RealField(53)</code> since you can not increase the precision of a number. Imagine the extreme case <code>RealField(2)(pi) = 3.0</code> to be multiplied with <code>RR(pi) = 3.14159265358979</code> would you expect Sage to silently give you as result <code>RR(pi)*RR(3.0) = 9.42477796076938</code> while <code>RR(pi)^2 = 9.86960440108936</code> ?</p>
https://ask.sagemath.org/question/27093/conversion-between-realfield/?comment=27102#post-id-27102Thanks for noticing, i updated the links.Fri, 12 Jun 2015 23:43:31 +0200https://ask.sagemath.org/question/27093/conversion-between-realfield/?comment=27102#post-id-27102