jaulib v1.4.1-14-g15926ba
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#include <jau/exceptions.hpp>
19
20namespace jau::math {
21
22 /** @defgroup Math Math Support
23 * Math Support Functionality, e.g. linear algebra, meta group
24 *
25 * Further support is coming from
26 * - \ref Integer
27 * - \ref ConstantTime
28 * - \ref Floats
29 *
30 * @{
31 */
32
33 /** Error types as specified by [C++ Math Error Handling](https://en.cppreference.com/w/cpp/numeric/math/math_errhandling) */
34 enum class math_error_t : uint16_t {
35 /** no math error */
36 none = 0,
37 /** See FE_INVALID, i.e. MathDomainError, std::domain_error : std::logic_error */
39 /** See FE_DIVBYZERO, i.e. MathDivByZeroError, std::domain_error : std::logic_error*/
41 /** See FE_OVERFLOW, i.e. MathOverflowError, std::overflow_error : std::runtime_error */
43 /** See FE_UNDERFLOW, i.e. MathUnderflowError, std::underflow_error : std::runtime_error */
45 /** See FE_INEXACT, i.e. MathInexactError, std::runtime_error */
47 /** undefined math error */
48 undefined = 1U << 15,
49 };
50 /** Returns std::string representation of math_error_t */
51 std::string to_string(const math_error_t v) noexcept;
52
54 private:
55 math_error_t m_error;
56
57 protected:
58 MathErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
59 : ExceptionBase("MathError("+to_string(err)+")", m, file, line), m_error(err) {}
60
61 public:
62 math_error_t error() const noexcept;
63 };
65 protected:
66 MathRuntimeErrorBase(math_error_t err, std::string const& m, const char* file, int line) noexcept
67 : MathErrorBase(err, m, file, line) {}
68 };
69
70 class MathError : public MathErrorBase, public std::exception {
71 public:
72 MathError(math_error_t err, std::string const& m, const char* file, int line) noexcept
73 : MathErrorBase(err, m, file, line), exception() {}
74
75 const char* what() const noexcept override {
76 return whole_message().c_str();
77 }
78 };
79
80 /** math_error_t::inexact */
81 class MathInexactError : public MathRuntimeErrorBase, public std::runtime_error {
82 public:
83 MathInexactError(std::string const& m, const char* file, int line) noexcept
84 : MathRuntimeErrorBase(math_error_t::inexact, m, file, line), runtime_error(whole_message()) {}
85
86 const char* what() const noexcept override {
87 return whole_message().c_str();
88 }
89 };
90
91 /** math_error_t::overflow */
92 class MathOverflowError : public MathRuntimeErrorBase, public std::overflow_error {
93 public:
94 MathOverflowError(std::string const& m, const char* file, int line) noexcept
95 : MathRuntimeErrorBase(math_error_t::overflow, m, file, line), overflow_error(whole_message()) {}
96
97 const char* what() const noexcept override {
98 return whole_message().c_str();
99 }
100 };
101
102 /** math_error_t::underflow */
103 class MathUnderflowError : public MathRuntimeErrorBase, public std::underflow_error {
104 public:
105 MathUnderflowError(std::string const& m, const char* file, int line) noexcept
106 : MathRuntimeErrorBase(math_error_t::underflow, m, file, line), underflow_error(whole_message()) {}
107
108 const char* what() const noexcept override {
109 return whole_message().c_str();
110 }
111 };
112
113 /** math_error_t::invalid */
114 class MathDomainError : public MathErrorBase, public std::domain_error {
115 protected:
116 MathDomainError(math_error_t err, std::string const& m, const char* file, int line) noexcept
117 : MathErrorBase(err, m, file, line), domain_error(whole_message()) {}
118
119 public:
120 MathDomainError(std::string const& m, const char* file, int line) noexcept
121 : MathErrorBase(math_error_t::invalid, m, file, line), domain_error(whole_message()) {}
122
123 const char* what() const noexcept override {
124 return whole_message().c_str();
125 }
126 };
127
128 /** math_error_t::div_by_zero, i.e. pole error */
130 public:
131 MathDivByZeroError(std::string const& m, const char* file, int line) noexcept
132 : MathDomainError(math_error_t::div_by_zero, m, file, line) {}
133 };
134
135 /**@}*/
136
137} // namespace jau
138
139#endif // JAU_MATH_ERROR_HPP_
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
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
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.