Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Coming from Maple I do not understand how the precision for numerical computations in Mathematica is specified. I understand that there are various options to commands such as WorkingPrecision and PrecisionGoal. But I would like to use the same precision (above machine precision) for a number of computations including matrix operations and the FindRoot command outside and inside of routines. Also I would like to specify the precision of the output.

share|improve this question
1  
You can look at the documentation for the functions listed when you evaluate ?$*Precision. You can do fixed precision calculations with Block[{$MaxPrecision=..., $MinPrecision=...}, ...] or set these globally to affect all functions that rely on it –  rm -rf Jun 4 '12 at 19:50
    
I tried setting $MinPrecision=20 already. Strangely I still get results with ScientificForm[%, 20] with just 16 digits. Do matrix computations and FindRoot depend on it? –  highsciguy Jun 4 '12 at 20:02
    
@highsciguy yes, but you have to be careful not to introduce machine-precision numbers at any point, which "poison" the result. That is, all numbers specified as decimals should have a precision annotation, e.g. 1.0`20. Also, you should be aware that some matrix decompositions are done in machine precision using LAPACK. –  Oleksandr R. Jun 4 '12 at 20:12
1  
I see. How do I tell mathematica that all numbers e.g. 1.5 are actually 20 Digits precision? SetPrecision on all numbers or add the `20 everywhere? –  highsciguy Jun 4 '12 at 20:16
1  
You can use either approach. SetPrecision will take the machine-precision value and extend it with base-2 zeros up to the required precision, which may not be what you want (since zeros in base 2 are not necessarily so in base 10; e.g. SetPrecision[1.9, 20] gives a result slightly less than 1.9). If you use the annotation, the zeros are taken to be in base 10 instead. Another possible approach is to use Rationalize. –  Oleksandr R. Jun 4 '12 at 21:53
show 4 more comments

1 Answer 1

up vote 5 down vote accepted

How do I tell mathematica that all numbers e.g. 1.5 are actually 20 Digits precision? SetPrecision on all numbers or add the `20 everywhere?

You could force this with $PreRead. This naive definition is likely inefficient and probably breaks a number of corner cases I have not considered, but here is a rough demonstration:

$PreRead = (# /. 
     s_String /; 
       StringMatchQ[s, NumberString] && 
        Precision@ToExpression@s == MachinePrecision :> s <> "`20." &);

3/1.5 + Pi/7

Precision[%]
2.4487989505128276055

20.0879

As Alexey notes this breaks if the machine number string already has a "NumberMark" after it e.g. 1.23`. One could use a more precise string replacement to avoid this.

A different approach is to process at the expression rather than box level, though this simple first attempt probably fails in some cases as well:

$Pre = Function[Null, 
  Unevaluated[#] /. r_Real?MachineNumberQ :> RuleCondition@SetPrecision[r, 25], 
  HoldAllComplete]

Now:

MachineNumberQ[2.2]
ToString[3.14]
False

"3.140000000000000124344979"
share|improve this answer
    
Your solution fails when the number entered has the NumberMark after digits, for example 1.5` . –  Alexey Popkov Dec 1 '13 at 19:24
    
@Alexey I never thought it would be robust as written. Let me see if I can improve it some. –  Mr.Wizard Dec 1 '13 at 20:42
    
Your second solution works in Mathematica 5.2 and 7.0.1 but not in v.8.0.4. Looks like a bug in $Pre in v.8.0.4. –  Alexey Popkov Dec 1 '13 at 21:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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