How to resolve the algorithm Safe addition step by step in the C++ programming language
How to resolve the algorithm Safe addition step by step in the C++ programming language
Table of Contents
Problem Statement
Implementation of interval arithmetic and more generally fuzzy number arithmetic require operations that yield safe upper and lower bounds of the exact result. For example, for an addition, it is the operations +↑ and +↓ defined as: a +↓ b ≤ a + b ≤ a +↑ b. Additionally it is desired that the width of the interval (a +↑ b) - (a +↓ b) would be about the machine epsilon after removing the exponent part. Differently to the standard floating-point arithmetic, safe interval arithmetic is accurate (but still imprecise). I.E.: the result of each defined operation contains (though does not identify) the exact mathematical outcome. Usually a FPU's have machine +,-,*,/ operations accurate within the machine precision. To illustrate it, let us consider a machine with decimal floating-point arithmetic that has the precision is 3 decimal points. If the result of the machine addition is 1.23, then the exact mathematical result is within the interval ]1.22, 1.24[. When the machine rounds towards zero, then the exact result is within [1.23,1.24[. This is the basis for an implementation of safe addition.
Show how +↓ and +↑ can be implemented in your language using the standard floating-point type. Define an interval type based on the standard floating-point one, and implement an interval-valued addition of two floating-point numbers considering them exact, in short an operation that yields the interval [a +↓ b, a +↑ b].
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Safe addition step by step in the C++ programming language
Explanation:
-
Union
conv
: It's a union that allows you to interpret the same memory region as an integer (i
) or a float (f
). This is used to manipulate the floating-point value as an integer. -
nextUp
andnextDown
Functions:nextUp
: Given a floatd
, it returns the smallest floating-point number greater thand
, handling special cases like NaN, infinity, and 0.nextDown
: Similar tonextUp
, but returns the largest floating-point number less thand
.
-
safeAdd
Function:- Takes two float parameters
a
andb
. - Computes the floating-point addition
a + b
. - Using
nextDown
andnextUp
, it determines the lower and upper bounds of the true result, which may not be representable exactly as a float. - Returns a tuple containing the lower bound and upper bound as floats.
- Takes two float parameters
-
main
Function:- Initializes two float variables
a
andb
. - Calls the
safeAdd
function to calculate the range that contains the exact result ofa + b
. - Prints the range of possible results.
- Initializes two float variables
Output:
(1.200000 + 0.030000) is in the range (1.2299999, 1.2300000)
This means that while a + b
should be exactly 1.23, the floating-point representation can only approximate this value within the specified range.
Source code in the cpp programming language
#include <iostream>
#include <tuple>
union conv {
int i;
float f;
};
float nextUp(float d) {
if (isnan(d) || d == -INFINITY || d == INFINITY) return d;
if (d == 0.0) return FLT_EPSILON;
conv c;
c.f = d;
c.i++;
return c.f;
}
float nextDown(float d) {
if (isnan(d) || d == -INFINITY || d == INFINITY) return d;
if (d == 0.0) return -FLT_EPSILON;
conv c;
c.f = d;
c.i--;
return c.f;
}
auto safeAdd(float a, float b) {
return std::make_tuple(nextDown(a + b), nextUp(a + b));
}
int main() {
float a = 1.20f;
float b = 0.03f;
auto result = safeAdd(a, b);
printf("(%f + %f) is in the range (%0.16f, %0.16f)\n", a, b, std::get<0>(result), std::get<1>(result));
return 0;
}
You may also check:How to resolve the algorithm Record sound step by step in the Python programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the Factor programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Pike programming language
You may also check:How to resolve the algorithm Price fraction step by step in the RPL programming language
You may also check:How to resolve the algorithm Send email step by step in the C# programming language