# Sample Multiple Precision Programs and Routines

Dr. David M. Smith
Professor of Mathematics (Emeritus)
Loyola Marymount University
Los Angeles, CA

When in doubt, use brute force.
--- Ken Thompson, Bell Labs

One of the advantages of multiple-precision is that the difficulties presented by problems that
are ill-conditioned or algorithms that are unstable can often be overcome by raising precision.

If we need a result correct to 15 digits and there is a simple algorithm that loses 10 digits to
cancellation error, it may be easier to write a program to use FM and carry 30 digits, instead of
spending the time to develop a sophisticated algorithm that can get 15 digits using double precision.

William Kahan is a numerical analysis guru whose web page http://www.cs.berkeley.edu/~wkahan/
has many papers showing in grisly detail some of the terrible things that can happen to the unwary
people who design or use numerical software.

In one of Kahan's articles, "How Futile are Mindless Assessments of Roundoff in Floating-Point
Computation?", he says,

"How can you separate numerically trustworthy results from the treacherous ones?"

"Without error-analysis, you can't. And if you can't, the simplest way to evade numerical
embarrassment is to perform computation carrying extravagantly more precision throughout
than you think necessary, and pray that it is enough. Usually somewhat more than twice the
precision you trust in the data and seek in the results is enough."

FM can make it relatively painless to check numerical results by re-running them with
extravagant precision.

Some of the .chk output files contain timing results. The times may vary when you run the programs.

The routines on this page are mostly brute force solutions and not state-of-the-art methods.
They all use the basic FM package, and should be compiled and linked in a similar way to the
other Fortran sample programs discussed in the FM user manual.

The actual files shown on this page are in this archive that contains all the FM files from this
website: FM_files.zip

The individual file pointers here display as pdf files for browsing. The longer files are cut
off after 20 pages.

Sample Routines Collected in One File

FM_Sample_Routines.f95 is a file that collects all the subroutines on this page into one file.
This is usually more convenient when using these routines, since only one object file is needed
when building programs that use them.

See the top of this file for a list of the routines.

Using FM for Special Functions

Some programs don't need multiple precision, but do need mathematical special functions
that are not part of the Fortran standard. In this case it is easy to set FM's precision level
to give full double precision accuracy and then use FM's version of these functions.

SampleDPfunctions.f95 is a small program with several examples using FM for non-Fortran
functions such as binomial coefficients, log integral, psi, polygamma, and incomplete gamma.

SampleDPfunctions.chk is the expected output from the program. Note that the first example
checks Fortran's built-in gamma function against FM. At the time this routine was created,
some Fortran compilers did not give correctly rounded results for all valid input arguments
to gamma, so your output might differ from this file in the first sample case.

Function Minimization or Maximization

fminFMv2.f95 is a program that includes FM_FIND_MIN, a multiple-precision max/min subroutine.

fminFMv2.chk is the output from the fminFMv2 program.

Derivatives

FprimeFM.f95 contains FM_FPRIME, a multiple-precision subroutine for nth derivatives
of real functions.

FprimeZM.f95 contains ZM_FPRIME, a multiple-precision subroutine for nth derivatives
of complex functions.

Integrals

IntegrateFM.f95 contains FM_INTEGRATE, a multiple-precision subroutine for definite
integrals of real functions.

SampleFMintegrate.f95 is a sample program using FM_INTEGRATE.

SampleFMintegrate.chk is the output from the SampleFMintegrate program.

OscillateFM.f95 is a program that uses FM_INTEGRATE to integrate sin(1/x) from 0 to 1.

OscillateFM.chk is the output from the OscillateFM program.

Ordinary Differential Equations

RK14FM.f95 contains FM_RK14, a multiple-precision subroutine for ordinary differential
equations (initial value problems).

DiffEqFM.f95 is a sample program using FM_RK14.

DiffEqFM.chk is the output from the DiffEqFM program.

Real Roots

SecantFM.f95 contains FM_SECANT, a multiple-precision root-finding subroutine.

RootsFM.f95 is a sample program using routines in files SecantFM and FprimeFM.

RootsFM.chk is the output from the RootsFM program.

Real Linear Systems

LinSolveFM.f95 contains FM_LIN_SOLVE, a routine for solving systems of linear equations.

InverseFM.f95 contains FM_INVERSE, a routine for finding the inverse of a real matrix.

GeneqFM.f95 contains FM_GENEQ, a routine that generates the system of normal equations
for solving a linear least-squares problem.

HarmonicFitFM.f95 is a sample program using routines in files GeneqFM and LinSolveFM.

HarmonicFitFM.chk is the output from the HarmonicFitFM program.

Complex Roots

SecantZM.f95 contains ZM_SECANT, a multiple-precision root-finding subroutine
for complex functions.

RootsZM.f95 contains ZM_ROOTS, a subroutine that tries to find many roots for complex functions.

AllRootsZM.f95 is a sample program using routines in file RootsZM.

AllRootsZM.chk is the output from the AllRootsZM program.

Complex Linear Systems and Eigenvalues

LinSolveZM.f95 contains ZM_LIN_SOLVE, a routine for solving systems of linear equations
having complex coefficients.

InverseZM.f95 contains ZM_INVERSE, a routine for finding the inverse of a complex matrix.

EigenSystemFM.f95 is a sample program that tries to find the complex eigenvalues and eigenvectors
of a real matrix.
It uses routines in files LinSolveFM, RootsZM, FprimeZM, LinSolveZM, and InverseZM.

EigenSystem.chk is the screen output from the EigenSystem program.

EigenSystem.out is the file output from the EigenSystem program.

Back to the main FM page