Theoretica
Scientific Computing
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_ERROR_H
7#define THEORETICA_ERROR_H
8
9#include <exception>
10
11#ifndef THEORETICA_NO_PRINT
12#include <sstream>
13#endif
14
15#include <string>
16#include <cerrno>
17#include <limits>
18#include "./constants.h"
19
20
21namespace theoretica {
22
23
25 enum class MathError : int {
26 None = 0x00,
27 DivByZero = 0x01,
28 OutOfDomain = 0x02,
29 OutOfRange = 0x04,
30 ImpossibleOperation = 0x08,
31 NoConvergence = 0x10,
32 InvalidArgument = 0x20
33 };
34
35
37 inline int to_errno(MathError err) {
38 switch(err) {
39 case MathError::None: return 0; break;
40 case MathError::DivByZero: return ERANGE; break;
41 case MathError::OutOfDomain: return EDOM; break;
42 case MathError::OutOfRange: return ERANGE; break;
43 case MathError::ImpossibleOperation: return EDOM; break;
44 case MathError::NoConvergence: return ERANGE; break;
45 case MathError::InvalidArgument: return EINVAL; break;
46 default: return 0; break;
47 }
48 }
49
50
52 inline const char* to_cstring(MathError err) {
53 switch (err) {
54 case MathError::None: return "No error"; break;
55 case MathError::DivByZero: return "Division by zero"; break;
57 return "An argument was out of the domain of the called function"; break;
59 return "A mathematically impossible operation was requested"; break;
60 case MathError::NoConvergence: return "The algorithm did not converge"; break;
61 case MathError::InvalidArgument: return "Invalid argument size or value"; break;
62 default: return "Unknown error"; break;
63 }
64 }
65
66
68 inline std::string to_string(MathError err) {
69 return to_cstring(err);
70 }
71
72
75 return std::numeric_limits<real>::quiet_NaN();
76 }
77
78
89 template<typename T>
90 inline bool is_nan(const T& x) {
91 return !(x == x);
92 }
93
94
97 return std::numeric_limits<real>::infinity();
98 }
99
100
102 inline bool is_inf(real x) {
103 return (x == inf()) || (x == -inf());
104 }
105
106
109 class math_exception : public std::exception {
110
111 private:
112 MathError err;
113 std::string func_name;
114 std::string file_name;
115 unsigned int code_line;
116 real val;
117
118 public:
119
120 math_exception(MathError a_err, const std::string& a_func_name,
121 const std::string& a_file_name, unsigned int a_code_line, real a_val)
122 : err(a_err), func_name(a_func_name), file_name(a_file_name),
123 code_line(a_code_line), val(a_val) {}
124
125 ~math_exception() = default;
126
127
129 inline const char* what() const noexcept {
130 return to_cstring(err);
131 }
132
133
135 inline MathError err_code() const {
136 return err;
137 }
138
139
141 inline std::string get_function_name() const {
142 return func_name;
143 }
144
145
147 inline std::string get_file_name() const {
148 return file_name;
149 }
150
151
153 inline unsigned int get_line_number() const {
154 return code_line;
155 }
156
157
159 inline real get_value() const {
160 return val;
161 }
162
163
164#ifndef THEORETICA_NO_PRINT
165
167 inline std::string to_string() const {
168
169 std::stringstream err_str;
170
171 err_str << file_name << "(" << code_line << "):";
172 err_str << func_name << "(" << val << "): ";
173 err_str << th::to_string(err);
174
175 return err_str.str();
176 }
177
178
180 inline operator std::string() {
181 return to_string();
182 }
183
184
187 inline friend std::ostream& operator<<(std::ostream& out, const math_exception& obj) {
188 return out << obj.to_string();
189 }
190
191#endif
192
193 };
194
195}
196
197
201
202
203// Only throw exceptions, without modifying errno
204#ifdef THEORETICA_ONLY_EXCEPTIONS
205
206#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
207 { throw theoretica::math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
208
209// Throw exceptions and modify errno
210#elif defined(THEORETICA_THROW_EXCEPTIONS)
211
212#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
213 { errno = theoretica::to_errno(EXCEPTION); \
214 throw math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
215
216// Modify errno only by default
217#else
218
219#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
220 { errno = theoretica::to_errno(EXCEPTION); }
221
222#endif
223
224
225// Output the value of an expression with additional information,
226// for debugging purposes.
227#define TH_DEBUG(VARIABLE) { \
228 std::cout << __FILE__ << ":" << __LINE__ << ": " \
229 << #VARIABLE << " = " << VARIABLE << std::endl; }
230
231
232#endif
A class for representing mathematical errors.
Definition error.h:109
friend std::ostream & operator<<(std::ostream &out, const math_exception &obj)
Stream the exception in string representation to an output stream (std::ostream)
Definition error.h:187
real get_value() const
Get a real value associated with the exception.
Definition error.h:159
unsigned int get_line_number() const
Get the line number at which the exception was thrown.
Definition error.h:153
std::string to_string() const
Get a string representation of the exception.
Definition error.h:167
std::string get_file_name() const
Get the name of the file in which the exception was thrown.
Definition error.h:147
MathError err_code() const
Get the error code associated with the exception.
Definition error.h:135
std::string get_function_name() const
Get the name of the throwing function.
Definition error.h:141
const char * what() const noexcept
Return a string describing the exception.
Definition error.h:129
Mathematical constants and default algorithm parameters.
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:170
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition error.h:90
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
int to_errno(MathError err)
Convert an MathError class enum to conventional errno codes.
Definition error.h:37
std::string to_string(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:68
MathError
Math error enumeration.
Definition error.h:25
@ InvalidArgument
Invalid argument.
@ OutOfDomain
Argument out of domain.
@ OutOfRange
Result out of range.
@ ImpossibleOperation
Mathematically impossible operation.
@ NoConvergence
Algorithm did not converge.
@ DivByZero
Division by zero.
bool is_inf(real x)
Check whether a real number is infinite.
Definition error.h:102
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:96
const char * to_cstring(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:52