On Thu, Dec 31, 2009 at 10:13 AM, eliot brenner <[email protected]> wrote: > Is there a way to make all floating point calculations within a > program or session occur to a fixed precision, say 500 digits? I know > how to issue commands like > > R500 = RealField(500) #set up 500 bit precision arithmetic > R500(pi) > > etc. for an individual calculation. But it would be preferable to be > able to issue a command that would result in all functions within the > program (such as sin, cos, etc.) evaluating to 500 bits precision, and > all intermediate steps being stored to 500 significant bits, and so > forth. The main reason is that I want the result of a complex program > to have a certain number of significant digits, and I am always afraid > that some rounding is occurring in some intermediate step. I want to > insure that no rounding occurs in an intermediate step, and also that
(Note: It's "ensure" not "insure".) > all functions evaluate to a fixed number of significant bits, without > any rounding occurring. There is nothing that completely does what you want. E.g., even if you do sage: R500 = RealField(500) sage: z = R500(pi); z 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813 Then nothing can prevent you (or a function you call) from doing this: sage: import math sage: math.sin(z) 1.2246467991473532e-16 And since math.sin is a call to the C-libraries sin function, it uses double precision. That said, in general in Sage if you do some operation that would cause precision loss, e.g., compute x+y, and either x or y has lower precision, then the default behavior is to make the result have the *lower* precision. Some people have argued for the opposite, i.e., for the result to have the *higher* precision, but I strongly disagree with that. As a result, if you do your calculation and at the end you get a number to 500 bits, then the precision wasn't lowered along the way due to mixing precisions. Regarding decimal literals, you might consider typing sage: RealNumber = lambda x: RealField(500)(x) since that will make it so they are automatically 500 bits: sage: 1.34929 1.34929000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 If you want to get a guaranteed result to a certain number of digits, you should investigate interval arithmetic by typing sage: RIF? sage: CIF? -- William -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
