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 [Maple Math] , or floating-point arithmetic, where numbers such as [Maple Math] 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;

[Maple Math]

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:

[Maple Math]

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 [Maple Math] where [Maple Math] is some function and [Maple Math] some number with [Maple Math] as [Maple Math] . Maple may be able to calculate it correctly if [Maple Math] is given as an exact expression. But if instead you give Maple an inexact floating-point approximation [Maple Math] of the correct [Maple Math] , it will tell you that [Maple Math] . This is in a sense perfectly correct (because [Maple Math] approaches some finite number as [Maple Math] ), but it is not what you wanted.

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

[Maple Math]

> b:= evalf(Pi,20);

[Maple Math]

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

[Maple Math]

Similarly, suppose A is a matrix with eigenvalue a , and you want to find the corresponding eigenvectors. You can use kernel(A-a) if a is exact, but this will not work if it is only a floating-point approximation.

> with(linalg):

Warning, new definition for norm

Warning, new definition for trace

> A:= matrix([[1,2,3],[3,1,4],[1,5,9]]);

[Maple Math]

> evalf(Eigenvals(A));

[Maple Math]

> kernel(A-.818268536);

[Maple Math]

In this case you can express the exact eigenvalue as a root of the characteristic polynomial, using RootOf (with a third argument that identifies which root is meant):

> kernel(A-RootOf(charpoly(A,t),t,.818268536));

[Maple Math]
[Maple Math]

> evalf(");

[Maple Math]

However, a better method for large matrices might be to use the optional second argument to Eigenvals to obtain all the eigenvectors, and extract the one you want.

> evalf(Eigenvals(A,'vects'));

[Maple Math]

> col(vects,1);

[Maple Math]

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

Maple Advisor Database, R. Israel 1997