C++ Library Extensions 2022.12.09
To help learn modern C++ programming
008-real.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2#include <tpf_safe_type.hpp>
3#include <cmath>
4
7
9{
10 /*
11 In case of integral types such as char, int, short, long, etc.
12 Division By Zero crashes your application.
13 */
14
15 int a = 5;
16 int b = 1;
17
18 auto r1 = a / b; // it causes your application to crash.
19 stream <<"Wow, not crashed! " << endl;
20
21 /*
22 In case of floating-point types such as float, double or long double,
23 Division-By-Zero is VALID (INFINITY) or INVALID (NaN).
24 */
25
26 double d1 = 5.0;
27 double d2 = 0.0;
28
29 auto r2 = d1 / d2; // is it valid operation, yes!!!!!
30
31 stream << "Wow, floating-point division by zero!! r2 = " << r2 << endl;
32
33 if(r2 == INFINITY)
34 {
35 stream << "Result is INFINITY" << endl;
36 }
37 else
38 {
39 stream << "Result is NOT INFINITY" << endl;
40 }
41
42 double d3 = 0.0;
43 double d4 = 0.0;
44
45 auto r3 = d3 / d4; // 0/0
46
47 /*
48 In case of floating-point operation,
49 your application does not crash. It works NORMALLY.
50 */
51 if(r3 != r3) // NaN != NaN, operation failed
52 {
53 stream << "Indeterminate State - INVALID floating point operation" << endl;
54 }
55 else
56 {
57 stream << "Determinate State - VALID floating-point operation" << endl;
58 }
59
60 stream << "The value of r3 = " << r3 << endl;
61
62}
63
64namespace types = tpf::types;
65namespace safe = tpf::safe_type;
66
67template<typename Type>
69safe_add(Type a, Type b)
70{
71 auto rlt = a + b;
72 if(rlt != rlt)
73 {
74 tpf::sstream os;
75
76 os << "floating-point operation failed - a = "
77 << a <<", b = " << b;
78
80 }
81
82 return rlt;
83}
84
85template<typename Type>
87safe_sub(Type a, Type b)
88{
89 auto rlt = a - b;
90 if(rlt != rlt)
91 {
92 tpf::sstream os;
93
94 os << "floating-point operation failed - a = "
95 << a <<", b = " << b;
96
98 }
99
100 return rlt;
101}
102
103template<typename Type>
105safe_mul(Type a, Type b)
106{
107 auto rlt = a * b;
108 if(rlt != rlt)
109 {
110 tpf::sstream os;
111
112 os << "floating-point operation failed - a = "
113 << a <<", b = " << b;
114
116 }
117
118 return rlt;
119}
120
121template<typename Type>
123safe_div(Type a, Type b)
124{
125 auto rlt = a / b;
126 if(rlt != rlt)
127 {
128 tpf::sstream os;
129
130 os << "floating-point operation failed - a = "
131 << a <<", b = " << b;
132
134 }
135
136 return rlt;
137}
138
140{
141 try
142 {
143 double d1 = INFINITY;
144 double d2 = INFINITY;
145 double d3 = -INFINITY;
146
147 auto r1 = safe_add(d1, d2); // infinity + infinity = infinity
148 stream << d1 << " + " << d2 <<" = " << r1 << endl;
149
150 auto r2 = safe_sub(d1, d3); // infinity - (-infinity) = infinity
151 stream << d1 << " - " << d3 <<" = " << r2 << endl;
152
153 double d4 = 1.0;
154 double d5 = 0.0;
155
156 auto r3 = safe_div(d4, d5); // 1.0 / 0.0 = INFINITY
157 stream << d4 << " / " << d5 <<" = " << r3 << endl;
158 }
159 catch(std::exception& e)
160 {
161 stream << e << endl;
162 }
163
164
165}
166int main()
167{
168 // examine_integer_float_difference();
170}
types::enable_if_real_number_t< Type > safe_mul(Type a, Type b)
Definition: 008-real.cpp:105
void examine_integer_float_difference()
Definition: 008-real.cpp:8
types::enable_if_real_number_t< Type > safe_sub(Type a, Type b)
Definition: 008-real.cpp:87
tpf::sstream stream
Definition: 008-real.cpp:5
types::enable_if_real_number_t< Type > safe_add(Type a, Type b)
Definition: 008-real.cpp:69
void examples_for_safe_floating_point_arithmetic()
Definition: 008-real.cpp:139
auto endl
Definition: 008-real.cpp:6
types::enable_if_real_number_t< Type > safe_div(Type a, Type b)
Definition: 008-real.cpp:123
int main()
Definition: 008-real.cpp:166
Defines safe type operation.
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
hidden::enable_if_real_number_t< Type, ReturnType > enable_if_real_number_t
Definition: tpf_types.hpp:5101
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
This file implements safe arithmetic.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.
Definition: tpf_types.hpp:1416