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, 20 Apr 2012 00:59:02 +0200Set global precision for reals?https://ask.sagemath.org/question/7887/set-global-precision-for-reals/Hello,
If this makes any difference, I am using the Sage Notebook.
I want to reuse a large piece of code. However, when I wrote it, the default precision seemed sufficient to me, and now I am encountering truncation errors. Can I specify a global precision at the beginning of the worksheet? My variable declarations are all like a,b=var('a,b').
Thanks!Thu, 20 Jan 2011 11:56:50 +0100https://ask.sagemath.org/question/7887/set-global-precision-for-reals/Comment by chaesloc for <p>Hello,</p>
<p>If this makes any difference, I am using the Sage Notebook.</p>
<p>I want to reuse a large piece of code. However, when I wrote it, the default precision seemed sufficient to me, and now I am encountering truncation errors. Can I specify a global precision at the beginning of the worksheet? My variable declarations are all like a,b=var('a,b').</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=22255#post-id-22255If the above is not possible, is it possible to change the default precision for hard-coded constants? For example, I want 12.34.precision() to return some value of my choice. Also, is it possible the result of an operation involving numbers of different precisions to automatically return a number of the higher precision?Thu, 20 Jan 2011 12:15:31 +0100https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=22255#post-id-22255Answer by cswiercz for <p>Hello,</p>
<p>If this makes any difference, I am using the Sage Notebook.</p>
<p>I want to reuse a large piece of code. However, when I wrote it, the default precision seemed sufficient to me, and now I am encountering truncation errors. Can I specify a global precision at the beginning of the worksheet? My variable declarations are all like a,b=var('a,b').</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?answer=12000#post-id-12000As far as I know there's no way to globally change the precision of computations over the real numbers. However, using `RealField` you can define variables (real numbers) that will only compute up to a desired precision. First note that Sage's "default" `RealField`, called `RR`, has 54 bits of precision:
sage: RR
Real Field with 53 bits of precision
sage: a = RR(1); b = RR(2)
sage: c = a+b
sage: c
3.00000000000000
sage: parent(c)
Real Field with 53 bits of precision
That last line shows that if you add two elements of `RR` you indeed get an element of `RR` in return. However, I can define lower precision real numbers like so:
sage: RF = RealField(10); RF
Real Field with 10 bits of precision
sage: a = RF(1); b = RF(2)
sage: c = a+b; c
3.0
sage: parent(c)
Real Field with 10 bits of precision
So the bottom line is that if you make sure that call of your calculations are in the field `RealField(prec)` then they will be with `prec` precision. Note that you can define matrices, vectors, polynomial rings, and what have you over `RealField(prec)` as well:
sage: RF = RealField(10)
sage: M = matrix(RF,[[1,0],[0,1]]); M
[ 1.0 0.00]
[0.00 1.0]
sage: parent(M)
Full MatrixSpace of 2 by 2 dense matrices over Real Field with 10 bits of precision
Finally, the constructor `ComplexField` is the complex analogue of `RealField`.Thu, 20 Jan 2011 12:47:24 +0100https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?answer=12000#post-id-12000Answer by Mike Hansen for <p>Hello,</p>
<p>If this makes any difference, I am using the Sage Notebook.</p>
<p>I want to reuse a large piece of code. However, when I wrote it, the default precision seemed sufficient to me, and now I am encountering truncation errors. Can I specify a global precision at the beginning of the worksheet? My variable declarations are all like a,b=var('a,b').</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?answer=12001#post-id-12001If you want to see what Sage does when you type in `12.34` into the command line, you can do
sage: preparse('12.34')
"RealNumber('12.34')"
You can see that it's just passing a string version of what you type in to the global function `RealNumber`. Currently, that just returns a RealNumber object with 53 bits of precision. You can replace that with whatever you want. For example,
sage: R = RealField(100); R
Real Field with 100 bits of precision
sage: RealNumber = R
sage: 12.34
12.340000000000000000000000000
sage: (12.34).parent()
Real Field with 100 bits of precision
Thu, 20 Jan 2011 16:55:07 +0100https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?answer=12001#post-id-12001Comment by asdrubalivan for <p>If you want to see what Sage does when you type in <code>12.34</code> into the command line, you can do</p>
<pre><code>sage: preparse('12.34')
"RealNumber('12.34')"
</code></pre>
<p>You can see that it's just passing a string version of what you type in to the global function <code>RealNumber</code>. Currently, that just returns a RealNumber object with 53 bits of precision. You can replace that with whatever you want. For example,</p>
<pre><code>sage: R = RealField(100); R
Real Field with 100 bits of precision
sage: RealNumber = R
sage: 12.34
12.340000000000000000000000000
sage: (12.34).parent()
Real Field with 100 bits of precision
</code></pre>
https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=19882#post-id-19882I was searching anything like this for a long time!! Thanks a lot :)
PS: Sorry for my english, that is not my mother tongue.Fri, 20 Apr 2012 00:59:02 +0200https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=19882#post-id-19882Comment by cswiercz for <p>If you want to see what Sage does when you type in <code>12.34</code> into the command line, you can do</p>
<pre><code>sage: preparse('12.34')
"RealNumber('12.34')"
</code></pre>
<p>You can see that it's just passing a string version of what you type in to the global function <code>RealNumber</code>. Currently, that just returns a RealNumber object with 53 bits of precision. You can replace that with whatever you want. For example,</p>
<pre><code>sage: R = RealField(100); R
Real Field with 100 bits of precision
sage: RealNumber = R
sage: 12.34
12.340000000000000000000000000
sage: (12.34).parent()
Real Field with 100 bits of precision
</code></pre>
https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=22254#post-id-22254This is much more concise than my response. :)Thu, 20 Jan 2011 19:56:08 +0100https://ask.sagemath.org/question/7887/set-global-precision-for-reals/?comment=22254#post-id-22254