6#ifndef THEORETICA_ERROR_H
7#define THEORETICA_ERROR_H
11#ifndef THEORETICA_NO_PRINT
46 default:
return 0;
break;
57 return "An argument was out of the domain of the called function";
break;
59 return "A mathematically impossible operation was requested";
break;
62 default:
return "Unknown error";
break;
75 return std::numeric_limits<real>::quiet_NaN();
97 return std::numeric_limits<real>::infinity();
103 return (x ==
inf()) || (x == -
inf());
109 class math_exception :
public std::exception {
113 std::string func_name;
114 std::string file_name;
115 unsigned int code_line;
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) {}
125 ~math_exception() =
default;
129 inline const char*
what() const noexcept {
164#ifndef THEORETICA_NO_PRINT
169 std::stringstream err_str;
171 err_str << file_name <<
"(" << code_line <<
"):";
172 err_str << func_name <<
"(" << val <<
"): ";
175 return err_str.str();
180 inline operator std::string() {
187 inline friend std::ostream&
operator<<(std::ostream& out,
const math_exception& obj) {
204#ifdef THEORETICA_ONLY_EXCEPTIONS
206#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
207 { throw theoretica::math_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, VALUE); }
210#elif defined(THEORETICA_THROW_EXCEPTIONS)
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); }
219#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION) \
220 { errno = theoretica::to_errno(EXCEPTION); }
227#define TH_DEBUG(VARIABLE) { \
228 std::cout << __FILE__ << ":" << __LINE__ << ": " \
229 << #VARIABLE << " = " << VARIABLE << std::endl; }
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
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition error.h:90
double real
A real number, defined as a floating point type.
Definition constants.h:207
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.
Definition error.h:32
@ OutOfDomain
Argument out of domain.
Definition error.h:28
@ OutOfRange
Result out of range.
Definition error.h:29
@ ImpossibleOperation
Mathematically impossible operation.
Definition error.h:30
@ NoConvergence
Algorithm did not converge.
Definition error.h:31
@ None
No error.
Definition error.h:26
@ DivByZero
Division by zero.
Definition error.h:27
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