Theoretica
Mathematical Library
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#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
10
11#include <exception>
12
13#ifndef THEORETICA_NO_PRINT
14#include <sstream>
15#endif
16
17#endif
18
19#include <string>
20#include <cerrno>
21#include <limits>
22#include "./constants.h"
23
24
25namespace theoretica {
26
27
29 enum class MathError : int {
30 None = 0x00,
31 DivByZero = 0x01,
32 OutOfDomain = 0x02,
33 OutOfRange = 0x04,
34 ImpossibleOperation = 0x08,
35 NoConvergence = 0x10,
36 InvalidArgument = 0x20
37 };
38
39
41 inline int to_errno(MathError err) {
42 switch(err) {
43 case MathError::None: return 0; break;
44 case MathError::DivByZero: return ERANGE; break;
45 case MathError::OutOfDomain: return EDOM; break;
46 case MathError::OutOfRange: return ERANGE; break;
47 case MathError::ImpossibleOperation: return EDOM; break;
48 case MathError::NoConvergence: return ERANGE; break;
49 case MathError::InvalidArgument: return EINVAL; break;
50 default: return 0; break;
51 }
52 }
53
54
56 inline const char* to_cstring(MathError err) {
57 switch (err) {
58 case MathError::None: return "No error"; break;
59 case MathError::DivByZero: return "Division by zero"; break;
61 return "An argument was out of the domain of the called function"; break;
63 return "A mathematically impossible operation was requested"; break;
64 case MathError::NoConvergence: return "The algorithm did not converge"; break;
65 case MathError::InvalidArgument: return "Invalid argument size or value"; break;
66 default: return "Unknown error"; break;
67 }
68 }
69
70
72 inline std::string to_string(MathError err) {
73 return to_cstring(err);
74 }
75
76
79 return std::numeric_limits<real>::quiet_NaN();
80 }
81
82
93 template<typename T>
94 inline bool is_nan(const T& x) {
95 return !(x == x);
96 }
97
98
101 return std::numeric_limits<real>::infinity();
102 }
103
104
106 inline bool is_inf(real x) {
107 return (x == inf()) || (x == -inf());
108 }
109
110
111#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
112
113 class math_exception : std::exception {
114
115 private:
116 MathError err;
117 std::string func_name;
118 std::string file_name;
119 unsigned int code_line;
120 real val;
121
122 public:
123
124 math_exception(MathError a_err, const std::string& a_func_name,
125 const std::string& a_file_name, unsigned int a_code_line, real a_val)
128
129 ~math_exception() = default;
130
131
133 inline const char* what() const noexcept {
134 return to_cstring(err);
135 }
136
137
139 inline MathError err_code() const {
140 return err;
141 }
142
143
145 inline std::string get_function_name() const {
146 return func_name;
147 }
148
149
151 inline std::string get_file_name() const {
152 return file_name;
153 }
154
155
157 inline unsigned int get_line_number() const {
158 return code_line;
159 }
160
161
163 inline real get_value() const {
164 return val;
165 }
166
167
168#ifndef THEORETICA_NO_PRINT
169
171 inline std::string to_string() const {
172
173 std::stringstream err_str;
174
175 err_str << file_name << "(" << code_line << "):";
176 err_str << func_name << "(" << val << "): ";
177 err_str << to_string(err);
178
179 return err_str.str();
180 }
181
182
184 inline operator std::string() {
185 return to_string();
186 }
187
188
191 inline friend std::ostream& operator<<(std::ostream& out, const math_exception& obj) {
192 return out << obj.to_string();
193 }
194
195#endif
196
197 };
198
199#endif
200
201}
202
203
207
208
209// Only throw exceptions, without modifying errno
210#ifdef THEORETICA_ONLY_EXCEPTIONS
211
212#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
213 { throw theoretica::math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
214
215// Throw exceptions and modify errno
216#elif defined(THEORETICA_THROW_EXCEPTIONS)
217
218#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
219 { errno = theoretica::to_errno(EXCEPTION); \
220 throw math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
221
222// Modify errno only by default
223#else
224
225#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
226 { errno = theoretica::to_errno(EXCEPTION); }
227
228#endif
229
230
231// Output the value of an expression with additional information,
232// for debugging purposes.
233#define TH_DEBUG(VARIABLE) { \
234 std::cout << __FILE__ << ":" << __LINE__ << ": " \
235 << #VARIABLE << " = " << VARIABLE << std::endl; }
236
237
238#endif
Mathematical constants and default algorithm parameters.
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:170
std::ostream & operator<<(std::ostream &os, const hdf5_node &node)
Prints the HDF5 file tree to a stream.
Definition hdf5.h:460
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:94
TH_CONSTEXPR Type make_error()
Create a number representing an error state, constructed from a NaN value.
Definition real_analysis.h:1322
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:78
int to_errno(MathError err)
Convert an MathError class enum to conventional errno codes.
Definition error.h:41
std::string to_string(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:72
MathError
Math error enumeration.
Definition error.h:29
@ 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:106
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:100
const char * to_cstring(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:56