Advice: Automatic simplification and evalf

The evalf function has a second, optional, argument which sets the number of digits to be used in the computation of the first argument. Without this second argument, the environment variable Digits sets the number of digits. However, in some cases the second argument is not effective. The reason is a very fundamental aspect of the way Maple processes input. Here is an example. First a calculation at the default Digits=10 .

> Digits:= 10: t:= 3.0:
1/t;

.3333333333

Now a calculation using evalf with its second parameter.

> evalf(1/t,20);

.33333333333333333333

However, if you replace t by its value 3.0:

> evalf(1/3.0,20);

.3333333333

What happened? Unlike nearly all Maple functions, the arguments to evalf are not evaluated first. This is why evalf(1/t,20) worked: the argument 1/t was passed to evalf unchanged, rather than being evaluated (which at Digits = 10 would have resulted in .3333333333). But even though evaluation is not done, there is always some automatic simplification applied to any input, and among the simplifications performed are arithmetic operations involving numbers. Thus 1/3.0 was immediately simplified to .3333333333 (at the current value Digits = 10 ) before evalf could operate on the expression.

There are two ways to work around this problem. One is to use variables (such as t above) to prevent the automatic simplifier from performing arithmetic. A second is to use the environment variable Digits instead of the second argument to evalf :

> Digits:= 20:
1/3.0;
Digits:= 10:

.33333333333333333333

See also: Digits , evalf , Exact vs. floating-point computations , Forcing floating-point arithmetic , Roundoff error , The meaning of Digits

Maple Advisor Database, R. Israel 1997