jaulib v1.3.6
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 (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_MATH_HPP_
26#define JAU_MATH_HPP_
27
28#include <stdexcept>
29#include <jau/int_math.hpp>
30#include <jau/basic_types.hpp>
31
32namespace jau::math {
33
34 /** @defgroup Math Math Support
35 * Math Support Functionality, e.g. linear algebra, meta group
36 *
37 * Further support is coming from
38 * - \ref Integer
39 * - \ref ConstantTime
40 * - \ref Floats
41 *
42 * @{
43 */
44
45 /** Error types as specified by [C++ Math Error Handling](https://en.cppreference.com/w/cpp/numeric/math/math_errhandling) */
46 enum class math_error_t : uint16_t {
47 /** no math error */
48 none = 0,
49 /** See FE_INVALID, i.e. MathDomainError, std::domain_error : std::logic_error */
51 /** See FE_DIVBYZERO, i.e. MathDivByZeroError, std::domain_error : std::logic_error*/
53 /** See FE_OVERFLOW, i.e. MathOverflowError, std::overflow_error : std::runtime_error */
55 /** See FE_UNDERFLOW, i.e. MathUnderflowError, std::underflow_error : std::runtime_error */
57 /** See FE_INEXACT, i.e. MathInexactError, std::runtime_error */
59 /** undefined math error */
60 undefined = 1U << 15,
61 };
62 /** Returns std::string representation of math_error_t */
63 std::string to_string(const math_error_t v) noexcept;
64
66 private:
67 math_error_t m_error;
68
69 protected:
70 MathErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
71 : ExceptionBase("MathError("+to_string(err)+")", m, file, line), m_error(err) {}
72
73 public:
74 math_error_t error() const noexcept;
75 };
77 protected:
78 MathRuntimeErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
79 : MathErrorBase(err, m, file, line) {}
80 };
81
82 class MathError : public MathErrorBase, public std::exception {
83 public:
84 MathError(math_error_t err, std::string const& m, const char* file, int line) noexcept
85 : MathErrorBase(err, m, file, line), exception() {}
86
87 const char* what() const noexcept override {
88 return whole_message().c_str();
89 }
90 };
91
92 /** math_error_t::inexact */
93 class MathInexactError : public MathRuntimeErrorBase, public std::runtime_error {
94 public:
95 MathInexactError(std::string const& m, const char* file, int line) noexcept
96 : MathRuntimeErrorBase(math_error_t::inexact, m, file, line), runtime_error(whole_message()) {}
97
98 const char* what() const noexcept override {
99 return whole_message().c_str();
100 }
101 };
102
103 /** math_error_t::overflow */
104 class MathOverflowError : public MathRuntimeErrorBase, public std::overflow_error {
105 public:
106 MathOverflowError(std::string const& m, const char* file, int line) noexcept
107 : MathRuntimeErrorBase(math_error_t::overflow, m, file, line), overflow_error(whole_message()) {}
108
109 const char* what() const noexcept override {
110 return whole_message().c_str();
111 }
112 };
113
114 /** math_error_t::underflow */
115 class MathUnderflowError : public MathRuntimeErrorBase, public std::underflow_error {
116 public:
117 MathUnderflowError(std::string const& m, const char* file, int line) noexcept
118 : MathRuntimeErrorBase(math_error_t::underflow, m, file, line), underflow_error(whole_message()) {}
119
120 const char* what() const noexcept override {
121 return whole_message().c_str();
122 }
123 };
124
125 /** math_error_t::invalid */
126 class MathDomainError : public MathErrorBase, public std::domain_error {
127 protected:
128 MathDomainError(math_error_t err, std::string const& m, const char* file, int line) noexcept
129 : MathErrorBase(err, m, file, line), domain_error(whole_message()) {}
130
131 public:
132 MathDomainError(std::string const& m, const char* file, int line) noexcept
133 : MathErrorBase(math_error_t::invalid, m, file, line), domain_error(whole_message()) {}
134
135 const char* what() const noexcept override {
136 return whole_message().c_str();
137 }
138 };
139
140 /** math_error_t::div_by_zero, i.e. pole error */
142 public:
143 MathDivByZeroError(std::string const& m, const char* file, int line) noexcept
144 : MathDomainError(math_error_t::div_by_zero, m, file, line) {}
145 };
146
147 /**@}*/
148
149} // namespace jau
150
151#endif // JAU_MATH_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.