Advice: Exact vs. floating-point computations

Maple can use exact arithmetic, where rational numbers are expressed as fractions and irrational numbers are expressed using symbolic expressions such as sqrt(2*Pi) , or floating-point arithmetic, where numbers such as 2.506628274 are expressed using a certain number of decimal places. It is often important to decide which of these is more appropriate in a particular problem. Often there is a tradeoff to consider: exact arithmetic avoids any problems of roundoff error, but can be much slower than floating-point and may result in very complicated expressions.

The following is an example of exact arithmetic. To save space, the result has been deleted from this help page: it is a fraction where both numerator and denominator have 15710 digits. This example should be no problem to calculate (except that it takes more than one screen to write), but it is easy to construct similar examples that will use up all available memory or take a very long time to compute.

> (1+1/4321)^4321;

Typically, however, all you really want is a few decimal places of the answer. For this purpose it is better to do the calculation in floating point. A numerical expression involving a number with a decimal point is evaluated in floating point (but see Forcing floating-point arithmetic ). Thus you could use

> (1+1./4321)^4321;

2.717968406

This is calculated very quickly, and the answer is in a much more convenient form.

The main disadvantage of floating point arithmetic is roundoff error. The calculation above is one in which the roundoff error is rather severe, due to the fact that we are adding a relatively small number ( 1./4321=.0002314279102 ) to a relatively large one ( 1 ), and losing significant digits in the process. This can be remedied by doing the calculation with a larger setting of the environment variable Digits , which controls the number of decimal digits used in the calculation.

> Digits:= 15:
r:= (1+1./4321)^4321:
Digits:= 10:
evalf(r);

2.717967352

There are some cases, however, in which even a very accurate floating-point approximation is no good, and exact arithmetic is necessary. Suppose, for example, we want to calculate Limit(f(x)*(x-a),x = a) where f(x) is some function and a some number with proc (f(x)) options operator, arrow; infinity end p... as proc (x) options operator, arrow; a end proc . Maple may be able to calculate it correctly if a is given as an exact expression. But if instead you give Maple an inexact floating-point approximation b of the correct a , it will tell you that Limit(f(x)*(x-b),x = b) = 0 . This is in a sense perfectly correct (because f(x) approaches some finite number as proc (x) options operator, arrow; b end proc ), but it is not what you wanted.

> f:= x -> 1/sin(x):
limit(f(x)*(x-Pi), x = Pi);

-1

> b:= evalf(Pi,20);

b := 3.1415926535897932385

> limit(f(x)*(x-b), x = b);

0

See also: Automatic simplification and evalf , Digits , evalf , Forcing floating-point arithmetic , Roundoff error , The meaning of Digits

Maple Advisor Database, R. Israel 1997