jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
math_error.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef JAU_MATH_ERROR_HPP_
13#define JAU_MATH_ERROR_HPP_
14
15#include <stdexcept>
16#include <jau/int_math.hpp>
17#include <jau/basic_types.hpp>
18
19namespace jau::math {
20
21 /** @defgroup Math Math Support
22 * Math Support Functionality, e.g. linear algebra, meta group
23 *
24 * Further support is coming from
25 * - \ref Integer
26 * - \ref ConstantTime
27 * - \ref Floats
28 *
29 * @{
30 */
31
32 /** Error types as specified by [C++ Math Error Handling](https://en.cppreference.com/w/cpp/numeric/math/math_errhandling) */
33 enum class math_error_t : uint16_t {
34 /** no math error */
35 none = 0,
36 /** See FE_INVALID, i.e. MathDomainError, std::domain_error : std::logic_error */
38 /** See FE_DIVBYZERO, i.e. MathDivByZeroError, std::domain_error : std::logic_error*/
40 /** See FE_OVERFLOW, i.e. MathOverflowError, std::overflow_error : std::runtime_error */
42 /** See FE_UNDERFLOW, i.e. MathUnderflowError, std::underflow_error : std::runtime_error */
44 /** See FE_INEXACT, i.e. MathInexactError, std::runtime_error */
46 /** undefined math error */
47 undefined = 1U << 15,
48 };
49 /** Returns std::string representation of math_error_t */
50 std::string to_string(const math_error_t v) noexcept;
51
53 private:
54 math_error_t m_error;
55
56 protected:
57 MathErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
58 : ExceptionBase("MathError("+to_string(err)+")", m, file, line), m_error(err) {}
59
60 public:
61 math_error_t error() const noexcept;
62 };
64 protected:
65 MathRuntimeErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
66 : MathErrorBase(err, m, file, line) {}
67 };
68
69 class MathError : public MathErrorBase, public std::exception {
70 public:
71 MathError(math_error_t err, std::string const& m, const char* file, int line) noexcept
72 : MathErrorBase(err, m, file, line), exception() {}
73
74 const char* what() const noexcept override {
75 return whole_message().c_str();
76 }
77 };
78
79 /** math_error_t::inexact */
80 class MathInexactError : public MathRuntimeErrorBase, public std::runtime_error {
81 public:
82 MathInexactError(std::string const& m, const char* file, int line) noexcept
83 : MathRuntimeErrorBase(math_error_t::inexact, m, file, line), runtime_error(whole_message()) {}
84
85 const char* what() const noexcept override {
86 return whole_message().c_str();
87 }
88 };
89
90 /** math_error_t::overflow */
91 class MathOverflowError : public MathRuntimeErrorBase, public std::overflow_error {
92 public:
93 MathOverflowError(std::string const& m, const char* file, int line) noexcept
94 : MathRuntimeErrorBase(math_error_t::overflow, m, file, line), overflow_error(whole_message()) {}
95
96 const char* what() const noexcept override {
97 return whole_message().c_str();
98 }
99 };
100
101 /** math_error_t::underflow */
102 class MathUnderflowError : public MathRuntimeErrorBase, public std::underflow_error {
103 public:
104 MathUnderflowError(std::string const& m, const char* file, int line) noexcept
105 : MathRuntimeErrorBase(math_error_t::underflow, m, file, line), underflow_error(whole_message()) {}
106
107 const char* what() const noexcept override {
108 return whole_message().c_str();
109 }
110 };
111
112 /** math_error_t::invalid */
113 class MathDomainError : public MathErrorBase, public std::domain_error {
114 protected:
115 MathDomainError(math_error_t err, std::string const& m, const char* file, int line) noexcept
116 : MathErrorBase(err, m, file, line), domain_error(whole_message()) {}
117
118 public:
119 MathDomainError(std::string const& m, const char* file, int line) noexcept
120 : MathErrorBase(math_error_t::invalid, m, file, line), domain_error(whole_message()) {}
121
122 const char* what() const noexcept override {
123 return whole_message().c_str();
124 }
125 };
126
127 /** math_error_t::div_by_zero, i.e. pole error */
129 public:
130 MathDivByZeroError(std::string const& m, const char* file, int line) noexcept
131 : MathDomainError(math_error_t::div_by_zero, m, file, line) {}
132 };
133
134 /**@}*/
135
136} // namespace jau
137
138#endif // JAU_MATH_ERROR_HPP_
const std::string & whole_message() const noexcept
Returns brief message and optional whole backtrace, i.e.
ExceptionBase(std::string &&type, std::string const &m, const char *file, int line) noexcept
MathDivByZeroError(std::string const &m, const char *file, int line) noexcept
const char * what() const noexcept override
MathDomainError(math_error_t err, std::string const &m, const char *file, int line) noexcept
MathDomainError(std::string const &m, const char *file, int line) noexcept
MathErrorBase(math_error_t err, std::string const &m, const char *file, int line) noexcept
math_error_t error() const noexcept
MathError(math_error_t err, std::string const &m, const char *file, int line) noexcept
const char * what() const noexcept override
const char * what() const noexcept override
MathInexactError(std::string const &m, const char *file, int line) noexcept
MathOverflowError(std::string const &m, const char *file, int line) noexcept
const char * what() const noexcept override
MathRuntimeErrorBase(math_error_t err, std::string const &m, const char *file, int line) noexcept
MathUnderflowError(std::string const &m, const char *file, int line) noexcept
const char * what() const noexcept override
math_error_t
Error types as specified by C++ Math Error Handling
std::string to_string(const math_error_t v) noexcept
Returns std::string representation of math_error_t.
@ underflow
See FE_UNDERFLOW, i.e.
@ overflow
See FE_OVERFLOW, i.e.
@ undefined
undefined math error
@ div_by_zero
See FE_DIVBYZERO, i.e.
@ inexact
See FE_INEXACT, i.e.
@ invalid
See FE_INVALID, i.e.