24 static bool found =
false;
35 tmp = (tmp * T(2)) + T(1);
52 static bool found =
false;
63 tmp = (tmp * T(2)) - T(1);
69 tmp = - max_limit<T>() - T(1);
111 #ifdef OSX_WORKAROUND
129 #ifdef OSX_WORKAROUND
153 value = max_limit<T>();
169 value = min_limit<T>();
186 value = max_limit<T>();
197 value = highest_value<T>();
210 value = -max_limit<T>();
211 if (value > min_limit<T>())
212 value = min_limit<T>();
225 value = lowest_value<T>();
247 if ((num < 0) && (num == min_limit<T>())) {
248 TRY_nomem(es =
"Absolute value is not containable by this type: ");
260 TRY_nomem(es =
"Absolute value is still negative: ");
307 m_num =
static_cast<T
>(0);
325 bool overflow =
false;
328 if (a_arg < static_cast<T>(0)) {
334 if (highest_value<T>() - a_arg <
m_num)
340 es =
"Addition overflow error detected: ";
347 es +=
estring(static_cast<int>(a_arg));
358 bool underflow =
false;
361 if (a_arg < static_cast<T>(0)) {
367 if (lowest_value<T>() < 0) {
368 if (
m_num < lowest_value<T>() + a_arg)
372 if (
m_num - lowest_value<T>() < a_arg)
379 es =
"Subtraction underflow error detected: ";
386 es +=
estring(static_cast<int>(a_arg));
397 bool overflow =
false;
399 if ((a_arg == 0) || (
m_num == 0) || (a_arg == 1) || (
m_num == 1)) {
404 if ((lowest_value<T>() < 0) && (
m_num < 0) && (a_arg < 0)) {
405 if (-highest_value<T>() > a_arg)
407 if (-(highest_value<T>() / a_arg) < -
m_num)
411 if ((lowest_value<T>() < 0) && (
m_num < 0) && (a_arg >= 0)) {
412 if (lowest_value<T>() / a_arg >
m_num)
416 if ((lowest_value<T>() < 0) && (
m_num >= 0) && (a_arg < 0)) {
417 if (lowest_value<T>() /
m_num > a_arg)
421 if ((lowest_value<T>() < 0) && (
m_num >= 0) && (a_arg >= 0)) {
422 if (highest_value<T>() / a_arg <
m_num)
426 if ((lowest_value<T>() >= 0) && (
m_num >= 0) && (a_arg >= 0)) {
427 if (highest_value<T>() / a_arg <
m_num)
438 es =
"Multiplication overflow error detected: ";
445 es +=
estring(static_cast<int>(a_arg));
458 if (a_arg == static_cast<T>(0)) {
461 es =
"Division by zero error detected: ";
468 es +=
estring(static_cast<int>(a_arg));
476 if ((
m_num < 0) && (a_arg < 0) && (num < 0)) {
479 es =
"Division result has incorrect sign: ";
486 es +=
estring(static_cast<int>(a_arg));
491 es +=
estring(static_cast<int>(num));
536 value = (
m_num == a_arg);
556 value = (
m_num != a_arg);
576 value = (
m_num < a_arg);
582 const bool operator<(const safe_num<T>& a_class)
const
586 value = (
m_num < a_class.value());
596 value = (
m_num > a_arg);
616 value = (
m_num <= a_arg);
622 const bool operator<=(const safe_num<T>& a_class)
const
626 value = (
m_num <= a_class.value());
636 value = (
m_num >= a_arg);
702 result.
add(a_class2);
758 a_class.
add(static_cast<T>(1));
770 a_class.
add(static_cast<T>(1));
779 a_class.
subtract(static_cast<T>(1));
791 a_class.
subtract(static_cast<T>(1));
798 std::ostream& operator<<(std::ostream& a_out, safe_num<T> a_class)
800 a_out << a_class.value();
void add(const T &a_arg)
Add a value.
T absolute(const T &a_num)
Return the absolute value of a numeric type.
Safely manipulate numbers without worryiung about over/underflow error.
Basic types definitions and templates.
static const T lowest_value(void)
Return 0 for unsigned types, or the maximum negative value that the type may hold.
An extended string class.
void divide(const safe_num< T > &a_class)
Divide by a safe_num.
const bool operator==(const safe_num< T > &a_class) const
Boolean operator.
safe_num< T > & operator+=(safe_num< T > a_class)
Arithmetic operator.
void add(const safe_num< T > &a_class)
Add a safe_num.
void subtract(const safe_num< T > &a_class)
Subtract a safe_num.
const T value(void) const
Return the value.
void assign(const safe_num< T > &a_class)
Assign a safe_num.
safe_num< T > operator/(safe_num< T > a_class1, safe_num< T > a_class2)
Arithmetic operator.
static const T max_limit()
A small set of numeric limits routines, since gcc prior to 3.x doesn't have numeric_limits.
const float max_limit< float >()
Return the largest possible number that a float may hold.
safe_num< T > operator+(safe_num< T > a_class1, safe_num< T > a_class2)
Arithmetic operator.
safe_num< T > & operator++(safe_num< T > &a_class)
Arithmetic operator.
safe_num< T > & operator-=(safe_num< T > a_class)
Arithmetic operator.
void assign(const T &a_arg)
Assign a value.
safe_num< T > operator*(safe_num< T > a_class1, safe_num< T > a_class2)
Arithmetic operator.
const bool operator>(const T &a_arg) const
Boolean operator.
void multiply(const T &a_arg)
Multiply by a value.
safe_num< T > & operator--(safe_num< T > &a_class)
Arithmetic operator.
safe_num< T > & operator*=(safe_num< T > a_class)
Arithmetic operator.
void divide(const T &a_arg)
Divide by a value.
const bool operator!=(const T &a_arg) const
Boolean operator.
safe_num(const safe_num &a_class)
C'tor.
const bool operator<=(const T &a_arg) const
Boolean operator.
safe_num< T > operator%(safe_num< T > a_class1, safe_num< T > a_class2)
Arithmetic operator.
#define INTERNAL_ERROR(e, s)
const bool operator>=(const T &a_arg) const
Boolean operator.
std::istream & operator>>(std::istream &a_in, safe_num< T > &a_class)
Arithmetic operator.
const double min_limit< double >()
Return the smallest positive number that a double may hold.
static const T highest_value(void)
Return the maximum possible value a type may hold.
static bool is_char(const T &a_arg)
Return true for any variable that is either a signed or unsigned char.
const float min_limit< float >()
Return the smallest positive number that a float may hold.
safe_num< T > operator-(safe_num< T > a_class1, safe_num< T > a_class2)
Arithmetic operator.
safe_num(const T a_num)
C'tor.
safe_num< T > & operator%=(safe_num< T > a_class)
Arithmetic operator.
void clear(void)
Clear the value.
void multiply(const safe_num< T > &a_class)
Multiply by a safe_num.
const double max_limit< double >()
Return the largest possible number that a double may hold.
const bool operator>(const safe_num< T > &a_class) const
Boolean operator.
const bool operator<(const T &a_arg) const
Boolean operator.
const bool operator>=(const safe_num< T > &a_class) const
Boolean operator.
void subtract(const T &a_arg)
Subtract a value.
const bool operator==(const T &a_arg) const
Boolean operator.
safe_num< T > & operator/=(safe_num< T > a_class)
Arithmetic operator.
const bool operator!=(const safe_num< T > &a_class) const
Boolean operator.
static const T min_limit()
Find the maximum limit for a type, equivalent to std::numeric_limits<T>.min() for systems that don't ha...