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.)
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:32nexpr = N[expr, prec], for a numeric expressionexpr, adjusts the precision of the numbers in its computations to make the accuracy ofnexprsatsifyAbs[(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 errorN[]calculates in this process is an approximation, an upper bound, or exact. – Goofy Feb 21 '24 at 14:38expris passed toN, 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:01NumberForm[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:10exprwas exact, which I failed to mention. Sorry about that. For inexact input,Ncannot use adaptive precision. It was that I was responding, and didn't make it clear. – Goofy Feb 21 '24 at 19:17SetAccuracyandSetPrecisionwill 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