It seems that Mathematica can handle very big integers accurately, but cannot do the same for floating numbers.

See the following code:

aa = 10^200;
aa = aa + 1; 
N[aa*Sqrt[2] - Round[aa*Sqrt[2]], 20]

The error message is

N::meprec: Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating -141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057148+1000000000000000000000000000000000000000000000000000000000000000000000000000000000<<36>>00000000000000000000000000000000000000000000000000000000000000000000000000000000001 <<1>>.

How to handle this kind of problem?

  • 1
    $\begingroup$ This usually happens when the result of subtracting two numbers results in something that is relatively very tiny compared to the original numbers. Consider it a warning that subtractive cancellation is happening. Anyway: Block[{$MaxExtraPrecision = 250}, N[aa*Sqrt[2] - Round[aa*Sqrt[2]], 20]]. $\endgroup$ – J. M.'s technical difficulties Apr 10 '17 at 11:10
  • $\begingroup$ You mean it is just a warning message, not an error message? $\endgroup$ – S. Kohn Apr 10 '17 at 11:26
  • 1
    $\begingroup$ Yes, it's a warning, but a very important one. It says that the result may be rubbish. In fact, in this specific case it is rubbish. There is not a very clear distinction between "error" and "warning" messages in Mathematica. The practical question is: can you ignore it? You definitely should not. $\endgroup$ – Szabolcs Apr 10 '17 at 11:29
  • 1
    $\begingroup$ You should look up catastrophic cancellation. When such things happen, most systems just give you a wrong result, without any warning. Mathematica goes further in helping you and warns you that the result may be rubbish. $\endgroup$ – Szabolcs Apr 10 '17 at 11:30

Oftentimes, to get a result with k digits of precision, the arithmetic needs to be done with more than k digits. Mathematica automatically increases the number of digits it uses to get a result with the requested precision. But there is a limit on how much it will increase it: $MaxExtraPrecision.

Why is there a limit? Consider examples like

 Sin[Pi/9] - Root[-3 + 36 #1^2 - 96 #1^4 + 64 #1^6 &, 4],

This triggers the same error. But no matter how much you increase the max extra precision, the error won't go away, because the exact result happens to be 0 here. Proving that it is zero is not trivial though, and Mathematica does not realize that it is so.

The solution, as J.M. said, is to allow for a higher precision increase during computations:

Block[{$MaxExtraPrecision = 250}, 
 N[aa*Sqrt[2] - Round[aa*Sqrt[2]], 20]]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.