United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
cxxtitle.gif (12116 bytes)
Compaq C++

Compaq C++
Using Compaq C++ for Tru64 UNIX Systems


Previous Contents Index

6.6 Numeric Limits Class

The Numeric Limits class represents information about arithmetic types. For each arithmetic type, the class provides a combination of the information that can be found in the following:

Not all of the information provided by the class members is meaningful for all types. Any value that is not meaningful for a type is set to 0 or false.

6.6.1 The numeric_limits Template Class

A synopsis of the numeric_limits class follows.


    template<class T> class numeric_limits { 
    public: 
      static const bool is_specialized = false; 
      inline static T min() throw(); 
      inline static T max() throw(); 
      static const int digits = 0; 
      static const int digits10 = 0; 
      static const bool is_signed = false; 
      static const bool is_integer = false; 
      static const bool is_exact = false; 
      static const int radix = 0; 
      inline static T epsilon() throw(); 
      inline static T round_error() throw(); 
      static const int min_exponent = 0; 
      static const int min_exponent10 = 0; 
      static const int max_exponent = 0; 
      static const int max_exponent10 = 0; 
      static const bool has_infinity = false; 
      static const bool has_quiet_NaN = false; 
      static const bool has_signaling_NaN = false; 
      static const bool has_denorm = false; 
      static const bool has_denorm_loss = false; 
      inline static T infinity() throw(); 
      inline static T quiet_NaN() throw(); 
      inline static T signaling_NaN() throw(); 
      inline static T denorm_min() throw(); 
      static const bool is_iec559 = false; 
      static const bool is_bounded = false; 
      static const bool is_modulo = false; 
      static const bool traps = false; 
      static const bool tinyness_before = false; 
      static const float_round_style round_style = round_toward_zero; 
    }; 
 

Specializations are provided for the following types:


 
    numeric_limits<char> 
    numeric_limits<signed char> 
    numeric_limits<unsigned char> 
    numeric_limits<short> 
    numeric_limits<int> 
    numeric_limits<long> 
    numeric_limits<unsigned short> 
    numeric_limits<unsigned int> 
    numeric_limits<unsigned long> 
    numeric_limits<float> 
    numeric_limits<double> 
    numeric_limits<long double> 
 

Note that the ANSI C++ September 1996 draft includes specializations for the bool and wchar_t types as follows:


 
numeric_limits<bool> 
numeric_limits<wchar_t> 
 

However, due to lack of support for these types in the current Compaq C++ compiler, specializations for them are not provided in the library.

6.6.2 Numeric Limits Class Member Functions

static T min() throw();

This function returns the minimum finite value. Its return value is equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, and so forth. For floating types with denormalization, the function returns the minimum positive normalized value. This function is meaningful for specializations where is_bounded is not equal to false, or where is_bounded and is_signed are both equal to false.

static T max() throw();

This function returns the maximum finite value. Its return value is equivalent to CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX, and so forth. The function is meaningful for all specializations in which is_bounded is not equal to false.

static T epsilon() throw();

This function returns machine epsilon. Its return value is equivalent to FLT_EPSILON, DBL_EPSILON, or LDBL_EPSILON. The function is meaningful for all floating point types.

static T round_error() throw();

This function returns the maximum rounding error.

static T infinity() throw();

This function returns the representation of positive infinity if it is available. The function is meaningful for all specializations for which has_infinity is true. It is required by specializations for which is_iec559 is true.

static T quiet_NaN() throw();

This function returns the representation of a quiet Not a Number if it is available. The function is meaningful for all specializations where has_quiet_NaN is not false. It is is required in specializations where is_iec559 is true.

static T signaling_NaN() throw();

This function returns the representation of a signaling Not a Number if it is available. The function is meaningful for all specializations where has_signaling_NaN is not false. It is required in specializations for which is_iec550 is true.

static T denorm_min() throw();

This function returns the minimum positive denormalized value. It is meaningful for all floating point types. It is required in specializations for which has_denorm is false.

6.6.3 Numeric Limits Class Data Members

const bool is_specialized;

Used to distinguish between types that have specializations and types that do not. If a specialization exists is_specialized is true, otherwise it is false.

const int digits;

This data member indicates the number of radix digits that can be represented. For integer types this is the number of nonsign bits in the representation. For floating point types this is the number of radix digits in the mantissa and is equivalent to FLT_MANT_DIG, DBL_MANT_DIG, or LDBL_MANT_DIG.

const int digits10;

This data member indicates the number of base 10 digits that can be represented without change. It applies to all specializations where is_bounded is true. For floating point types digits10 is equivalent to FLT_DIG, DBL_DIG, or LDBL_DIG.

const bool is_signed;

This data member indicates whether a type is signed or not. It applies to all specializations.

const bool is_integer;

This data member indicates whether a type is an integer or not. It applies to all specializations.

const bool is_exact;

This data member is true if a type has an exact representation. All integer types are exact. It applies to all specializations.

const int radix;

For floating types this data member specifies the base or radix of the exponent and is equivalent to FLT_RADIX. For integer types it specifies the base of the representation.

const int min_exponent;

This data member indicates the minimum negative integer such that radix raised to the power of one less than that integer is a normalized floating point number. It applies to all floating types and is equivalent to FLT_MIN_EXP, DBL_MIN_EXP, or LDBL_MIN_EXP.

const int min_exponent10;

This data member indicates the minimum negative integer such that 10 raised to that power is in the range of normalized floating point numbers. It applies to all floating types and is equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP, or LDBL_MIN_10_EXP.

const int max_exponent;

This data member indicates the maximum positive integer such that radix raised to the power one less than that integer is a representable finite floating point number. It applies to all floating types and is equivalent to FLT_MAX_EXP, DBL_MAX_EXP, or LDBL_MAX_EXP.

const int max_exponent10;

This data member indicates the maximum positive integer such that 10 raised to that power is in the range of representable finite floating point numbers. It applies to all floating types and is equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP, or LDBL_MAX_10_EXP.

const bool has_infinity;

This data member is true if the type has a representation for positive infinity. It applies to all floating types.

const bool has_quiet_NaN;

This data member is true if the type has a quiet (that is, nonsignaling) Not a Number. It applies to all floating types.

const bool has_signaling_NaN;

This data member is true if the type supports a signaling Not a Number. It applies to all floating types.

const bool has_denorm;

This data member is true for types that support denormalized values. It applies to all floating types.

const bool has_denorm_loss;

This data member is true if loss of accuracy is detected as a denormalization loss, rather than as an inexact result.

const bool is_iec559;

This data member is true if the type adheres to the International Electrotechnical Commission (IEC) 559 standard. It applies to all floating types.

const bool is_bounded;

This data member is true if the set of values represented by the type is finite. All built-in types are bounded. It applies to all specializations.

const bool is_modulo;

This data member is true if a type is modulo. A type is modulo if you can add two positive numbers and have a result that wraps around to a number that is less than the two operands. This data member is usually false for floating types, true for unsigned integers and true for signed integers on most machines. It applies to all specializations.

const bool traps;

This data member is true if the type supports trapping. It applies to all specializations.

const bool tinyness_before;

This data member is true if tinyness is detected before rounding. It applies to all floating types.

const float_round_style round_style;

This data member indicates the rounding style for the type. It applies to all floating types. Specializations for integer types return round_toward_zero. See Section 6.6.4 for possible values.

6.6.4 Numeric Limits Class Data Types

enum float_round_style

This data type is used to indicate the rounding mode for floating point arithmetic. It contains the following values:
Value Rounding Style
round_indeterminate Indeterminable
round_toward_zero Toward zero
round_to_nearest To the nearest representable value
round_toward_infinity Toward infinity
round_neg_infinity Toward negative infinity

6.7 The auto_ptr Class

The auto_ptr class provides a simple class for smart pointers; it stores a pointer to an object obtained by way of the new operator and deletes that object when the auto_ptr object is destroyed. The auto_ptr class provides semantics of strict ownership. After construction an auto_ptr object owns the object whose pointer it holds. When an instantiation of auto_ptr is copied, ownership of the object is transferred to the destination auto_ptr. The behavior is undefined if more than one instantiation of auto_ptr owns the same object at the same time.

The ANSI C++ September 1996 auto_ptr class makes use of member templates for its copy constructor and assignment operator. Because the current Compaq C++ compiler does not support member templates, auto_ptr does not make use of them for copy construction or assignment operations.

6.7.1 The auto_ptr Member Functions

explicit auto_ptr(T* p=0) throw();

Constructs an object of class auto_ptr<T>. The pointer held by the auto_ptr class is initialized to p. The p variable points to an object of type T or a class derived from T for which delete p is defined and accessible, or p is a null pointer.

auto_ptr(const auto_ptr<T>& a) throw();

Constructs an object of class auto_ptr<T>. The argument a is copied to *this. If a owns the held pointer, ownership is transferred to *this.

auto_ptr<T>& operator=(const auto_ptr<T>& rhs) throw();

The argument rhs is copied to *this. If rhs owns the held pointer, ownership is transferred to *this. If *this already owns a pointer (not equal to the pointer owned by rhs), that held pointer is deleted first.

~auto_ptr();

If *this owns the held pointer, the pointer is deleted.

T& operator*() const throw();

Returns a reference to the object to which the underlying held pointer points.

T* operator->() const throw();

Returns the underlying held pointer.

T* get() const throw();

Returns the underlying held pointer.

T* release() throw();

Returns the underlying held pointer after releasing ownership of it.

6.8 The Standard Exception Library

The standard exception classes are all classes derived from the exception class and are used in the standard library to report errors detected during program execution. A simple example of using this class follows:


 
#include <stdexcept> 
#include <string> 
 
int main() { 
        try { 
                string s("abc"); 
                if (s.at(4)=='d') ; // out of range 
        } 
        catch(const exception& e) { 
                cout << e.what() << endl; 
        } 
        catch(...) { 
                cout << "unknown exception" << endl; 
        } 
} 
 

The output from this example is as follows:


position beyond end of string 

Users may also use the standard exception hierarchy to report errors in their own classes.

6.8.1 Types of Standard Exceptions

All standard exceptions are rooted at the base class exception. No standard library errors throw an object of type exception (they are always of a more specific or derived type). This class is simply used as a base class when a user wants to catch all standard exceptions, as in the previous example.

Six classes are derived from exception:

The logic_error class has four derived classes:

The runtime_error class has three derived classes:

6.8.2 The exception Member Functions

exception() throw();

Constructs an object of class exception.

exception(const exception&) throw();

Copies an exception object.

exception& operator=(const exception&) throw();

Assigns an exception object.

virtual ~exception() throw();

Destroys the exception object.

virtual const char* what() const throw();

Returns a null-terminated byte string that contains information about the type of error. The contents of the string are implementation dependent. In the Compaq C++ library the string contains an indication of the problem---that is, string index out of range or invalid string size parameter.

6.8.3 Derived Exception Classes

All of the standard exception classes are derived from class exception; thus, they inherit all of the exception class member functions. The only difference is in the constructor---all have an overridden constructor that takes a string argument; that is:


class logic_error: public exception { 
public: 
 logic_error(const string& what_arg); 
}; 
 
class domain_error: public logic_error { 
public: 
 domain_error(const string& what_arg); 
}; 
 
class invalid_argument: public logic_error { 
public: 
 invalid_argument(const string& what_arg); 
}; 
 
class length_error: public logic_error { 
public: 
 length_error(const string& what_arg); 
}; 
 
class out_of_range: public logic_error { 
public: 
 out_of_range(const string& what_arg); 
}; 
 
class runtime_error: public exception { 
public: 
 runtime_error(const string& what_arg); 
}; 
 
class range_error: public runtime_error { 
public: 
 range_error(const string& what_arg); 
}; 
 
class overflow_error: public runtime_error { 
public: 
 overflow_error(const string& what_arg); 
}; 
 
class underflow_error: public runtime_error { 
public: 
 underflow_error(const string& what_arg); 
}; 

6.9 The Complex Math Library

A complex number has a real part and an imaginary part. The standard complex library utilizes templates, so that any underlying type can be used to represent these parts. Template specializations are provided for float, double, and long double.

6.9.1 Example of Use

A simple example of use is:


#include <complex> 
 
int main() { 
 complex<double> c1(1,1), c2(3.14,3.14); 
 cout << "c2/c1: " << c2/c1 << endl; 
} 

Note that you must explicitly link in the math library when using the complex library on Tru64 UNIX, that is:


cxx prog.cxx -lm 

6.9.2 Complex Math Member Functions

complex(const T& re = T(), const T& im = T());

Constructs an object of type complex with the real part set to re and the imaginary part set to im.

complex(const complex<T>& c);

Constructs a complex number by copying all elements from c.

complex<T>& operator=(const complex<T>& c);

Assigns c to *this and returns *this.

complex<T>& operator=(const T& c);

Assigns c to the real part of *this. Sets the imaginary part of *this to zero and returns *this.

complex<T>& operator+=(const complex<T>& c);

Adds c to *this and then returns the result.

complex<T>& operator-=(const complex<T>& c);

Subtracts c from *this and then returns the result.

complex<T>& operator*=(const complex<T>& c);

Multiplies c and *this and then returns the result.

complex<T>& operator/=(const complex<T>& c);

Divides *this by c and then returns the result.

T real();

Returns the real part of *this.

T imag();

Returns the imaginary part of *this.

6.9.3 Complex Math Nonmember Functions

template <class T>

T real(const complex<T>& c);

Returns the real part of c.

template <class T>

T imag(const complex<T>& c);

Returns the imaginary part of c.

template <class T>

T abs(const complex<T>& c);

Returns the magnitude of c.

template <class T>

T norm(const complex<T>& c);

Returns the squared magnitude of c.

template <class T>

T arg(const complex<T>& c);

Returns the phase angle of c.

template <class T>

complex<T> conj(const complex<T>& c);

Returns the complex conjugate of c.

template <class T>

complex<T> polar(const T& rho, const T& theta = 0);

Returns the complex value corresponding to a complex number whose magnitude is rho and whose phase angle is theta.

template <class T>

complex<T> cos(const complex<T>& c);

Returns the complex cosine of c.

template <class T>

complex<T> cosh(const complex<T>& c);

Returns the complex hyperbolic cosine of c.

template <class T>

complex<T> exp(const complex<T>& c);

Returns the complex base e exponential of c.

template <class T>

complex<T> log(const complex<T>& c);

Returns the complex natural (base e) logarithm of x.

template <class T>

complex<T> log10(const complex<T>& c);

Returns the complex common (base 10) logarithm of x.

template <class T>
complex<T> pow(const complex<T>& x, int y); template <class T> complex<T> pow(const complex<T>& x, int y); template <class T> complex<T> pow(const complex<T>& x, const complex<T>& y);

template <class T>
complex<T> pow(const complex<T>& x, const T& y); template <class T> complex<T> pow(const T& x, const complex<T>& y);

Returns the complex power of base x raised to the y-th power.

template <class T>

complex<T> sin(const complex<T>& c);

Returns the complex sine of c.

template <class T>

complex<T> sinh(const complex<T>& c);

Returns the complex hyperbolic sine of c.

template <class T>

complex<T> sqrt(const complex<T>& c);

Returns the complex square root of c, in the range of the right half-plane.

template <class T>

complex<T> tan(const complex<T>& c);

Returns the complex tangent of c.

template <class T>

complex<T> tanh(const complex<T>& c);

Returns the complex hyperbolic tangent of c.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement