11#ifndef PBAT_MATH_INTEGER_ARITHMETIC_CHECKS_H
12#define PBAT_MATH_INTEGER_ARITHMETIC_CHECKS_H
29template <std::
integral Integer>
35 auto constexpr max = std::numeric_limits<Integer>::max();
36 auto constexpr min = std::numeric_limits<Integer>::lowest();
58template <std::
integral Integer>
64 auto constexpr max = std::numeric_limits<Integer>::max();
65 auto constexpr min = std::numeric_limits<Integer>::lowest();
66 bool const bSameSign = (a > 0 && b > 0) or (a < 0 && b < 0);
71 return std::abs(a) > std::abs(max / b);
77 return -std::abs(a) < (min / std::abs(b));
88template <std::
integral Integer>
91 auto constexpr max = std::numeric_limits<Integer>::max();
92 if constexpr (std::is_signed_v<Integer>)
110template <std::
integral Integer>
135 throw std::overflow_error(
"Negation overflow");
147 throw std::overflow_error(
"Addition overflow");
159 throw std::overflow_error(
"Multiplication overflow");
182 template <std::
integral OtherInteger>
190 operator Integer()
const {
return value; }
Math related functionality.
Definition Concepts.h:19
bool NegationOverflows(Integer a)
Checks if the operation is not in the range of values representable by the type of Integer.
Definition IntegerArithmeticChecks.h:89
bool MultiplyOverflows(Integer a, Integer b)
Checks if the operation is not in the range of values representable by the type of Integer.
Definition IntegerArithmeticChecks.h:59
bool AddOverflows(Integer a, Integer b)
Checks if the operation is not in the range of values representable by the type Integer.
Definition IntegerArithmeticChecks.h:30
The main namespace of the library.
Definition Aliases.h:15
Wrapper around integer types that throws when integer overflow is detected.
Definition IntegerArithmeticChecks.h:112
SelfType operator*(SelfType rhs) const
Multiplication operator.
Definition IntegerArithmeticChecks.h:156
SelfType operator/(OtherInteger rhs) const
Division operator.
Definition IntegerArithmeticChecks.h:183
SelfType operator+(SelfType rhs) const
Addition operator.
Definition IntegerArithmeticChecks.h:144
Integer const & operator*() const
Const dereference operator.
Definition IntegerArithmeticChecks.h:126
OverflowChecked< Integer > SelfType
Instance type.
Definition IntegerArithmeticChecks.h:113
Integer value
Underlying integer value.
Definition IntegerArithmeticChecks.h:192
Integer & operator*()
Dereference operator.
Definition IntegerArithmeticChecks.h:120
SelfType operator-(SelfType rhs) const
Subtraction operator.
Definition IntegerArithmeticChecks.h:168
SelfType operator/(SelfType rhs) const
Division operator.
Definition IntegerArithmeticChecks.h:175
SelfType operator-() const
Negation operator.
Definition IntegerArithmeticChecks.h:132