# 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.

edit retag close merge delete

Sort by » oldest newest most voted

You should have a look at the documentation about coercion, for example, see http://doc.sagemath.org/html/en/tutor... and http://doc.sagemath.org/html/en/refer...

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 ?

more

Thanks 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.

( 2015-06-12 19:09:20 +0200 )edit

Thanks for noticing, i updated the links.

( 2015-06-12 23:43:31 +0200 )edit