FPAvisual

FPAvisual

FPAvisual has two versions.

After downloading the needed file to a directory, use an appropriate method to decompress the file. All files will be saved in the current directory and no new subdirectory will be created. The Linux-64 executable file is FPAvisual and Windows executable is FPAvisual.exe.

Introduction

Due to binary representation and finite precision, the floating-point data type in computer programs is not equivalent to real numbers in mathematics. Major differences occur when real numbers are converted into binary and are truncated to contain only the maximum number of digits allowed by the specific representation and precision. Programs that contain floating- point variables may produce incorrect results, or worse, may contain assumptions that will produce unpredictable results when the computing environment or the program parameters change. However, many students in science and engineering do not realize how program correctness may be impacted when floating-point arithmetic is used. Therefore, it is important to include floating-point concepts in computing curricula.

Roots

Solving a quadratic equation is a common problem in many fields of science and engineering. However, rounding and cancellation may affect the results. Roots demonstrates their effect and provides possible solutions using four approaches. The first and the third solutions are the “naive” floating-point implementations of the standard formula for the two roots in single precision and double precision, respectively. The second is the cancellation solution with single-precision floating-point format that improves the result in one case that is described below. The last one is the solution using a high-precision floating-point format, which is treated as a ground-truth solution.


Roots

Pentagon

The Pentagon component illustrates the inaccuracies caused by rounding. We chose the pentagon problem in geometric computation because it provides an easy way to understand problems in an interactive environment. The pentagon problem involves defining a pentagon with its five vertices and iteratively using “in” and “out” operations to get back to the original pentagon. The “in” operation computes a smaller pentagon by computing the intersection points of the lines joining the vertices of the original pentagon. This operation can be repeated on the new pentagon to form an even smaller one. The “out” extends the opposite sides of the pentagon to get the intersection points and form a larger pentagon. In Figure 2, the result of the “in” operation on the red pentagon is the blue one while the result of “out” operation on the blue pentagon is the red one. At each iteration, the vertices used for computation are calculated from the previous iteration. In theory, as long as the numbers of “in”s and “out”s are equal, the new pentagon should be exactly the same as the initial one. However, because of rounding, this may not always be true. The “in” and “out” operations require computing the intersection point of two lines. If the computation is inaccurate, the error may propagate to the next iteration, which will make the resulting pentagon dramatically different from the original one. This difference allows students to see the significance of accumulated floating-point errors and the difficulty of calculating the intersection point of two almost parallel lines.


Pentagon

Associative Law

Associative Law demonstrates a situation when changing the order of computations of algebraically equivalent formulas may yield very different results. Give a value R and an initial value X0 , calculate:

Xn = (R + 1) × Xn−1 − R × Xn−1 × Xn−1.

In this formula, R denotes the growing rate. When R is larger than 2.57, the rounding-off error dramatically influences the accuracy and causes the associative law to fail.

We calculate the formula with five different orders:

Xn = (R + 1) × Xn−1 − R × (Xn−1 × Xn−1),

Xn = (R + 1) × Xn−1 − (R × Xn−1) × Xn−1,

Xn = ((R + 1) − R × Xn−1) × Xn−1,

Xn = R × Xn−1 + (1 − R × Xn−1) × Xn−1,

Xn = Xn−1 + R × (Xn−1 − Xn−1 × Xn−1).

In theory, these orders will generate the same set of Xn . However, because of the rounding-off error, they won’t. Figure 1 (a) shows Xn s where R equals 3 and X0 is 0.5, we can see that the five orders begin to differentiate at iteration 24. Figure 1 (b) shows the differences of the five orders with the first one as the base line.


Associative Law

Sine Function

In this component, FPAvisual uses an infinite series approximation to the sine function to reveal some critical concepts in iterative summations of floating-point numbers. The sine function sin(x) can be evaluated using the infinite Taylor series as follows:

sin(x) = x - x3 / (3!) + x5 / (5!) - x7 / (7!) + ... = SUMn=0inf(-1)n x2n+1 / ((2n+1)!)

where x is in radian. If x is very large or small, x2n+1 may overflow or underflow when n is large. One way to overcome this problem is to reduce the user input (in degrees) to the [0, 90) interval using the following trigonometric identities:

(1)Since sin(x) = -sin(x), if x is less than 0, we just use -sin(|x|).

(2)Since sin(x) has a period of 360, we can reduce x to [0, 360) by letting x = x % 360.

(3)Since sin(x + 180) = -sin(x), if x is larger than or equal to 180, we may reduce x to [0, 180) by letting x = x - 180, and changing the sign of the computed result.

(4)Since sin(180 - x) = sin(x), if x is in [90, 180), we may reduce x to [0, 90) by letting x = 180 - x.

There is another place where overflow or underflow may occur. If sin(x) is computed in a naive way by directedly evaluating each term x2n+1 /((2n + 1)!), it is very likely that (2n+1)! will quickly cause integer overflow. In the implementation, we utilized a floating-point value to store it. A safer approach is to update a term from the previous one:

x2n+1 / ((2n + 1)!) = x2n-1 / ((2n - 1)!) × x2 / ((2n)(2n+1))

The third potential error occurs in the summation of terms with alternating sign values due to cancellation. To solve this problem, FPAvisual utilizes the positive-negative and Kahan summation algorithms.

Including the direct way in each aspect, we have 2 ways of using x (i.e., original x and reduced x), 2 ways of calculating the terms (i.e., direct evaluation and term update) and 3 ways for summation (i.e., direct, positive-negative and Kahan), therefore we have 2 x 2 x 3 = 12 ways. For comparison, FPAvisual adds the result of the sin(x) library function.


Sine Function

User Guide

In this "User Guide", the description of major functions are be listed.

Roots

The "a", "b" and "c" can be set in the "Input" section. Press key "Enter" or the button "Calculate" will calculate the roots with four solutions. The pink color indicates the value is different from the other solutions.

Pentagon

The "Operations" section consists of "In", "Out" operations. "In" connects the intersection points of the pentagon to form a small pantagon. "Out" revises "In" operation. "Clear" clears all the "In" and "Out" operations. The "Mode" section consists of three modes. "Animated example" enables rendering the examples in an animated fashion. "Square and epsilon" and "Enter five points manually" allows user to select the initial pentagon. On the display screen on the left, dragging the mouse while the left button is pushed will zoom in to any region in the screen. Clicking the right button of the mouse will cancel one "Zooming In" operation.

Associative Law

The "Input" section allows user to type in the input. Press key "Enter" or the button "Calculate" will calculate the values of each iteration automatically. On the display screen on the left, dragging the mouse while the left button is pushed will zoom in to any region in the screen. Clicking the right button of the mouse will cancel one "Zooming In" operation.

Sine Function

The "Input" section allows user to type in the input. Press key "Enter" or the button "Calculate" will calculate the sine values of each iteration automatically. "Error comparison" allows user to select one approach as a base function and visualize the differences between the rest and it. On the display screen on the left, dragging the mouse while the left button is pushed will zoom in to any region in the screen. Clicking the right button of the mouse will cancel one "Zooming In" operation. Clicking on the label in the lower section of the display window will select (unselect) the approaches to render.

Publication

Yi Gu, Nilufer Onder, Ching-Kuang Shene, Chaoli Wang,
FPAvisual: A Tool for Visualizing the Effects of Floating-Point Finite-Precision Arithmetic,
Proceedings of American Society for Engineering Education Annual Conference 2014, Indianapolis, IN, Jun 2014.
(Accepted.)
Click here for a PDF copy.