Ask Your Question

Set global precision for reals?

asked 2011-01-20 11:56:50 +0100

chaesloc gravatar image


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').


edit retag flag offensive close merge delete



If 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?

chaesloc gravatar imagechaesloc ( 2011-01-20 12:15:31 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2011-01-20 16:55:07 +0100

Mike Hansen gravatar image

If you want to see what Sage does when you type in 12.34 into the command line, you can do

sage: preparse('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
sage: (12.34).parent()
Real Field with 100 bits of precision
edit flag offensive delete link more


This is much more concise than my response. :)

cswiercz gravatar imagecswiercz ( 2011-01-20 19:56:08 +0100 )edit

I was searching anything like this for a long time!! Thanks a lot :) PS: Sorry for my english, that is not my mother tongue.

asdrubalivan gravatar imageasdrubalivan ( 2012-04-20 00:59:02 +0100 )edit

answered 2011-01-20 12:47:24 +0100

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

edit flag offensive delete link more

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


Asked: 2011-01-20 11:56:50 +0100

Seen: 4,594 times

Last updated: Jan 20 '11