Jump to content

Safe addition: Difference between revisions

Line 289:
size inf
</pre>
 
=={{header|C++}}==
{{trans|C#}}
<lang cpp>#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;
}</lang>
{{out}}
<pre>(1.200000 + 0.030000) is in the range (1.2299998998641968, 1.2300001382827759)</pre>
 
=={{header|C#|C sharp}}==
1,452

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.