Ask Your Question
0

Conversion between RealField

asked 2015-06-12 17:33:51 +0100

candide gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-06-12 18:04:18 +0100

tmonteil gravatar image

updated 2015-06-12 23:44:23 +0100

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 ?

edit flag offensive delete link more

Comments

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.

candide gravatar imagecandide ( 2015-06-12 19:09:20 +0100 )edit

Thanks for noticing, i updated the links.

tmonteil gravatar imagetmonteil ( 2015-06-12 23:43:31 +0100 )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: 2015-06-12 17:33:51 +0100

Seen: 599 times

Last updated: Jun 12 '15