Message Boards Message Boards

0
|
5948 Views
|
8 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Increase the precision of calculations without SetPrecision in every step?

Posted 6 years ago

Hallo, I have a very long code including definition of some functions. I try to increase the precision of my calculations. I used MinPrecsion but it didn't help. I also used :

Unprotect[MachinePrecision];
MachinePrecision = 164;
Protect[MachinePrecision];

Also didn't help. Is there a solution for this problem other that adding SetPrecision in every step of the code. TIA.

POSTED BY: Maha Youssef
8 Replies

It would be easier to offer concrete assistance if you post a minimal example of the issue you are trying to work around.

POSTED BY: Daniel Lichtblau

First, you can't change MachinePrecision without changing machines. :)

Second, look up $MinPrecision and $MaxPrecision. You can set them both to 164 to fix the amount of arbitrary precision. However, you still have to avoid MachinePrecision numbers, because those parameters do not apply to machine-precision or exact numbers, only to arbitrary-precision numbers.

POSTED BY: Michael Rogers
Anonymous User
Anonymous User
Posted 6 years ago

In addition to what was already said: you might read more of the Mathematica Book (4.0 may be shorter to find this in)

While you can set accuracy and precision and number base (and overall machine precision?) for calculations (there's shortcut notation for it too)... Mathematica (unlike MATLAB or some competitors) can do: Arbitrary Precision Calculations.

You get "kicked" by machine precision only when you ask to see the number as "not a fraction". So you'd work with fractions until the bitter end that you want a numeric display. Only in very rare lengthy calculations would MM fail to be able to keep your numbers faithfully represent fractions due to memory limits (it might try to simplify if memory limits hit: but might be impossible to simplify to continue - since some large divisions can't be found or simplified or held in memory).

So: Rule #1 is don't ask for machine precision (non-fraction display) until you want to PRINT the number (ie on paper, or temporarily to view it with N[]). Equivalently, don't the the "faster" "Numeric Solve" (ie, Newtonian Approximation) if you can solve symbolically.

POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 6 years ago

Fractions that don't simplify are a topic: you need prime numbers, perhaps database of, and cannot always find one or there may not be one. If they fit in memory that's fine - but some things like 1/x can make them grow and "need shortening" (but if your not using arbitrary precision - this problem is faced at EACH calculation, thus the any error multiplies, as your physics books told you in that lesson).

In that case of even arbitrary precision, Mathematica can be forced to "choose a side" (add or take 1 to a long fraction a/b part so that it can find a prime number and simplify it to save memory) (you'd like see a numerical warning if this FAILED to happened).

So here's the interesting part! If you use an equation that's "supposed to be unitary and accurate", and if you allow 1/x fractions to get out of hand by "mis-using them" (not being careful they do not get large), now lets say your "program" turns on whether (the least decimal) is Greater[.5]: you might be steered the wrong way! Your input might be SMOOTH, and hit a few points of incorrect answers, and be correct %99.999999 of the time!

Interesting isn't it? Texas schools offered (offers) classes on it: ie, how to do correct calculations (never getting steered wrong) using limited hand-calculators.

It's interesting but too rare for most people to "worry about". Yet ... don't forget that problem exists.

POSTED BY: Anonymous User
Posted 6 years ago

Thank you.

POSTED BY: Maha Youssef
Posted 6 years ago

Thank you. The point is, in the middle of the code I have to switch to numeric values. If I keep it symbolic it will be much more comploicated and the run time keep for long. That is why I need a high precision calcuations.

POSTED BY: Maha Youssef

Maha,

Its hard to say in the abstract without an example, however, you can easily do arbitrary precision math by setting the precision of all constants with the prime symbol (key above Tab on keyboard) or use SetPrecision[] for an expression.

In[1]:= SetPrecision[2*a + 3*b + c, 20]

Out[1]= 2.0000000000000000000 a + 3.0000000000000000000 b + c

Is this what you are asking?

Regards,

Neil

POSTED BY: Neil Singer
Posted 6 years ago

Actually, No. This is step by step precision. What I'm looking for is to set a precision for the whole file in one step at the begining, so that all the calculations are automatically using this precision.

POSTED BY: Maha Youssef
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract