6#ifndef THEORETICA_ERROR_H
7#define THEORETICA_ERROR_H
9#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
13#ifndef THEORETICA_NO_PRINT
50 default:
return 0;
break;
61 return "An argument was out of the domain of the called function";
break;
63 return "A mathematically impossible operation was requested";
break;
66 default:
return "Unknown error";
break;
79 return std::numeric_limits<real>::quiet_NaN();
101 return std::numeric_limits<real>::infinity();
107 return (x ==
inf()) || (x == -
inf());
111#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
113 class math_exception : std::exception {
117 std::string func_name;
118 std::string file_name;
119 unsigned int code_line;
123 math_exception(
MathError a_err,
const std::string& a_func_name,
124 const std::string& a_file_name,
unsigned int a_code_line,
real a_val)
125 : err(a_err), func_name(a_func_name), file_name(a_file_name),
126 code_line(a_code_line), val(a_val) {}
128 ~math_exception() =
default;
132 inline const char* what() const noexcept {
144 inline std::string get_function_name()
const {
150 inline std::string get_file_name()
const {
156 inline unsigned int get_line_number()
const {
162 inline real get_value()
const {
167#ifndef THEORETICA_NO_PRINT
172 std::stringstream err_str;
174 err_str << file_name <<
"(" << code_line <<
"):";
175 err_str << func_name <<
"(" << val <<
"): ";
181 err_str <<
"An argument was out of the domain of the called function";
break;
183 err_str <<
"A mathematically impossible operation was requested";
break;
186 default: err_str <<
"Unknown error";
break;
189 return err_str.str();
194 inline operator std::string() {
201 inline friend std::ostream& operator<<(std::ostream& out,
const math_exception& obj) {
202 return out << obj.to_string();
220#ifdef THEORETICA_ONLY_EXCEPTIONS
222#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
223 { throw theoretica::math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
225#define TH_MATH_ERROR_R(F_NAME, VALUE, EXCEPTION) \
226 TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
229#elif defined(THEORETICA_THROW_EXCEPTIONS)
231#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
232 { errno = theoretica::to_errno(EXCEPTION); \
233 throw math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
238#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
239 { errno = theoretica::to_errno(EXCEPTION); }
246#define TH_DEBUG(VARIABLE) { \
247 std::cout << __FILE__ << ":" << __LINE__ << ": " \
248 << #VARIABLE << " = " << VARIABLE << std::endl; }
Mathematical constants and default algorithm parameters.
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:161
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:198
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition error.h:94
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