Package org.apfloat.samples
Three different versions of an application for calculating π are
included. The simplest, Pi
runs on one
computer using one processor (and one thread) only. PiParallel
executes multiple threads in parallel and has vastly better performance
on multicore computers. Finally, PiDistributed
can use multiple separate computers for calculating pi with even
greater processing power.
As a curiosity, two applets are provided for running Pi
and PiParallel
through a graphical user
interface: PiApplet
and PiParallelApplet
,
correspondingly. These programs can also be run as standalone
Java applications: PiGUI
and PiParallelGUI
.
Compared to the C++ version of apfloat, the Java version pi calculation program is usually just as fast. Even in worst cases the Java version achieves roughly 50% of the performance of the assembleroptimized C++ versions of apfloat. Modern JVMs are nearly as efficient as optimizing C++ compilers in code generation. The advantage that JVMs have over native C++ compilers is obviously that the JVM generates optimal code for every target architecture and runtime profile automatically, from an intermediate portable binary executable format. With C++, the source code must be compiled and profiled manually for every target architecture, which can be difficult and tedious.
On multicore computers the Java parallel pi calculator is often significantly faster than the C++ parallel version. The same applies to the distributed pi calculator. Multithreaded and distributed applications are more efficient to implement in Java due to C++'s historical lack of standard libraries for threading and networking.

ClassDescriptionSimple JavaBean to hold one
Apfloat
.Class for running anOperation
in the background in a separate thread.Class to executeOperation
s locally.Operation<T>Interface for implementing arbitrary operations to be executed.Interface for implementing objects that can executeOperation
s.Server for executingOperation
s from remote calls.Calculates pi using four different algorithms.Abstract base class for the binary splitting series.Class for implementing the binary splitting algorithm.Indicates progress of the pi calculation using the binary splitting algorithm.Terms for the binary splitting series.Calculates pi using the Borweins' quartic algorithm.Chudnovskys' algorithm terms for the binary splitting series.Basic class for calculating pi using the Chudnovskys' binary splitting algorithm.Calculates pi using the GaussLegendre algorithm.Ramanujan's algorithm terms for the binary splitting series.Basic class for calculating pi using the Ramanujan binary splitting algorithm.Applet for calculating pi using four different algorithms.Graphical AWT elements for calculating pi using four different algorithms.Interface to indicate an error status in the application.Calculates pi using a cluster of servers.Distributed version of the binary splitting algorithm.Class for calculating pi using the distributed Chudnovskys' binary splitting algorithm.Class for calculating pi using the distributed Ramanujan's binary splitting algorithm.RemoteOperationExecutor that implements the weight property.AWT client application for calculating pi using four different algorithms.Calculates pi using multiple threads in parallel.Parallel version of the binary splitting algorithm.Class for calculating pi using the parallel Chudnovskys' binary splitting algorithm.Class for calculating pi using the parallel Ramanujan's binary splitting algorithm.Class to execute operations while settingApfloatContext.setNumberOfProcessors(int)
to some value.Applet for calculating pi using multiple threads in parallel.Graphical AWT elements for calculating pi using multiple threads in parallel.AWT client application for calculating pi using multiple threads in parallel.Class to call anOperationServer
to executeOperation
s remotely.