2

Here are some things I believe are correct reading the documentation:

If I do N[someArithmetics,{Infinity, 20}] I am guaranteed that the computation done (symbolized by someArithmetics) should give me a result that is correct up to 20 digits after the comma (or something around that).

This does not mean it will be what I expect up to 20 digits, since my input could be only correct up to, say, 10. However, if my input has infinite precision and 20 accuracy, I should get 20 correct digits.

According to the docs:

SetAccuracy just sets the precision of numbers, while N works adaptively

So what does this mean? My test with this number is inconclusive:

NumberForm[N[10000000.010, {Infinity, 20}], 20]
NumberForm[SetAccuracy[10000000.010, 30], 20]

with output

1.000000001*10^7
1.0000000009999999776*10^7

Clearly, telling MMA to 'compute' 10000000.010 to 20 digits gives me not 20 digits for N. Accuracy and Precision reveal:

8.95459 (*Accuracy*)
MachinePrecision (*Precision*)

Now SetAccuracy behaves even weirder. I expect MMA to fill up my number with 0s until the 20th digit after the comma. However, it just does... whatever?

This makes computations such as:

NumberForm[
 N[SetAccuracy[10000000.01, 20] - 
   SetAccuracy[10000000.02, 20], {Infinity, 20}], 20]

give absolute wild (and certainly wrong) results: -0.0099999997764825821 (the wrongness is of course in the first significant digit.) The reason for this result is of course that MMA does not fill my number with 0, but rather seemingly random things.

So assuming my input is correct up to 20 digits after the comma, how do I guarantee that my result is correct with 20 digits after the comma?

And please explain what is going on with SetAccuracy and N (and SetPrecision behaves the same in the above examples.)

  • Btw: The general lore seems to be indeed that if I start with something not machineprecision, but rather defined to 20 digits after comma, using N[#,{Infinity, 20}] should give me a correct result, as seen here https://mathematica.stackexchange.com/questions/10624/confused-by-apparent-inconsistent-precision?rq=1 or here https://mathematica.stackexchange.com/questions/27505/when-can-i-assume-that-all-decimal-digits-returned-by-mathematica-are-provably-c?rq=1 . But how do I reliably generate such precise inputs? SetAccuracy certainly doesn't work. – Confuse-ray30 Feb 21 '24 at 12:32
  • (Can someone tell me how to shorten links?) Since I don't want to type twenty zeroes after every number I input, and using fractions is not desirable here (some numbers aren't just simply typed in, but rather results from, say NDSolve). – Confuse-ray30 Feb 21 '24 at 12:34
  • nexpr = N[expr, prec], for a numeric expression expr, adjusts the precision of the numbers in its computations to make the accuracy of nexpr satsify Abs[(nexpr - expr)/expr] <= 10^-prec. At least in theory. Some functions are hard to compute. Zero is hard (or impossible) to compute. I also do not know whether the error N[] calculates in this process is an approximation, an upper bound, or exact. – Goofy Feb 21 '24 at 14:38
  • @Goofy but how would mathematica know what ´expr´ is if I wrap ´N´ around a arithmetic operation? After all for this information to be available, MMA would have to 'calculate expr infinitely accurate' first and then make this expression. But if it did that, why don't just give me ´expr´? – Confuse-ray30 Feb 21 '24 at 18:30
  • expr is passed to N, which then works with it internally. — "MMA would have to 'calculate expr infinitely accurate' first and then make this expression." The mathematics of many functions is well-known, including error bounds for methods of approximating them. The docs for many numeric functions state that the function "can be evaluated to arbitrary numerical precision." This means they have an algorithm that converges to the exact value and they know how many steps it takes to be within a given error. Sometimes there are problems, though. – Goofy Feb 21 '24 at 19:01
  • @Goofy So if I understand correctly, up to some corner cases (which for the sake of argument, I dont have) N[#,prec] indeed gives a relative error of less than 10^-prec, and N[#,{Infinity, acc}] gives me acc-digits of correct results if wrapped around a function like + and *? – Confuse-ray30 Feb 21 '24 at 19:08
  • @Goofy But could you explain to me the result I obtained when doing: NumberForm[N[10000000.010, {Infinity, 20}], 20]? It says that this is still MachinePrecision and I only have about 9 digits of Accuracy. But clearly, this can't be an edge case of N, right? – Confuse-ray30 Feb 21 '24 at 19:10
  • See https://mathematica.stackexchange.com/questions/180907/why-does-n-not-upgrade-precision. I assumed expr was exact, which I failed to mention. Sorry about that. For inexact input, N cannot use adaptive precision. It was that I was responding, and didn't make it clear. – Goofy Feb 21 '24 at 19:17
  • 1
    SetAccuracy and SetPrecision will fill in with zero bits rather than digits. Compare `In[198]:= RealDigits[ SetAccuracy[10000000.02, 12] - SetAccuracy[10000000.01, 12], 2]

    Out[198]= {{1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -6}

    In[199]:= RealDigits[SetAccuracy[.01, 12], 2]

    Out[199]= {{1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1}, -6}`. One is a truncation of the other.

    – Daniel Lichtblau Feb 21 '24 at 21:24
  • Try N[10000000.010`20, 20] – Bill Watts Feb 21 '24 at 23:46
  • @DanielLichtblau that makes so much more sense! – Confuse-ray30 Feb 22 '24 at 07:51

0 Answers0