
June 7, 1999 10:10 owltex Sheet number 22 Page number 757 magenta black How to: Understand and Use Standard Libraries F By its very nature, the library provided with a programming language is a mixed bag. P.J. Plauger The Standard C Library, p. x F.1 Functions C++ inherits many free (non-class) functions from C. A function library is a collection of cohesive functions that have a common domain. For example, the header file <cmath> imports mathematical functions, the file <cctype> imports character functions, and the library <cstdlib> imports “standard” algorithms like the C-based functions atoi and atof. In addition to the function libraries inherited from C, C++ includes several standard class libraries. In particular, the Standard Template Library, or STL, provides implementations of functions, algorithms, and container classes like vector. We use some of the ideas from STL, for example in the class tvector and in the sorting functions of sortall.h, but a complete discussion of STL is beyond the scope of this book. Complete though terse information on STL is available in [Str97]; a description of why the library works as it does and a wonderful book on generic programming is [Aus98]. The function libraries imported using header files of the form <cXXX> are in std namespace. For a brief introduction to namespaces see Section A.2.3 in How to A. Func- tions in the global namespace are imported using <XXX.h>. For example, use <cmath> for functions in the std namespace, but <math.h> for functions in the global names- pace. Older libraries/environments typically support only the .h versions of the function libraries. F.1.1 The Library <cmath> Functions in the standard math library, <cmath>, are given in Table F.1. On older sys- tems this library is called <math.h>. All trigonometric functions use radian measure. See the functions in mathutils.h, Program G.9 for functions to convert between degrees and radians. Most of the functions in <cmath> are described sufficiently in Table F.1. The arguments to atan2 are presumed to be x- and y-coordinates, so that, atan2(1,1) is the same as atan2(3,3) or atan(π/4). 757 June 7, 1999 10:10 owltex Sheet number 23 Page number 758 magenta black 758 Appendix F How to: Understand and Use Standard Libraries Table F.1 Some functions in <cmath> function name prototype returns double fabs (double x) absolute value of x double abs (double x) absolute value of x (C++only) double log (double x) natural log of x double log10 (double x) base-ten log of x double sin (double x) sine of x (x in radians) double cos (double x) cosine of x (x in radians) double tan (double x) tangent of x (x in radians) double asin (double x) arc sine of x [−π/2,π=2] double acos (double x) arc cosine of x [0,π] double atan (double x) arc tangent of x [−π/2,π=2] double atan2 (double x, atan(x/y) double y) double sinh (double x) hyperbolic sine of x double cosh (double x) hyperbolic cosine of x double tanh (double x) hyperbolic tangent of x double pow (double x, xy double y) √ double sqrt (double x) x, square root of x double fmod (double d, floating-point remainder d/m double m) double ldexp (double d, d*pow(2,i) int i) double floor (double x) largest integer value ≤ x double ceil (double x) smallest integer value ≥ x F.1.2 The Library <cctype> The functions in <cctype> operate on char values, they’re summarized in Table F.2. On older systems this library is called <ctype.h>. You would expect functions with the prefix is, such as islower and isalnum, to have return type bool. However, to ensure compatibility with both C and C++ code, many libraries use integer values for the return type of these predicates in <cctype>. These boolean-valued functions return some nonzero value for true, but this value is not necessarily one. All the functions use int parameters, but arguments are expected to be in the range of legal char values. F.2 Constants and Limits Several header files import constants and functions that encapsulate platform-specific limits on the maximum and minimal values of different built-in types. Unfortunately, the C++ standard does not require an int to be represented by 32 bits, nor a double June 7, 1999 10:10 owltex Sheet number 24 Page number 759 magenta black F.2 Constants and Limits 759 Table F.2 Some functions in <cctype> function prototype returns true when int isalpha(int c) c is alphabetic (upper or lower case) int isalnum(int c) c is alphabetic or a digit int islower(int c) c is a lowercase letter int isdigit(int c) c is a digit character ’0’-’9’ int iscntrl(int c) c is a control character int isprint(int c) c is printable character including space int ispunct(int c) c is a punctuation (printable, not space, not alnum) int isspace(int c) c is any white-space character, ’ ’,’\t’,’\n’,’\v’, ’\r’,’\f’ int isupper(int c) c is an uppercase letter returns int tolower(int c) lowercase equivalent of c int toupper(int c) uppercase equivalent of c to be represented by 64 bits, although these are the standard sizes on 32-bit computers and are the standard sizes used in languages like Java. F.2.1 Limits in <climits> The header file <climits> (or <limits.h>) imports the constants shown in oldlim- its.cpp, Program F.1. However, the value INT_MIN, for example, is almost certainly a preprocessor #define rather than a C++ constant. Although these constants are sim- ple to use, consider using the constants and classes defined in <limits>, whose use is shown in Program F.2 below. Program F.1 oldlimits.cpp #include <iostream> #include <iomanip> // for setw #include <climits> #include <string> using namespace std; // illustrates range of values for integral types const int FIELD_SIZE = 13; // size of field for output chunk void Print(const string& type, long low, unsigned long high); int main() June 7, 1999 10:10 owltex Sheet number 25 Page number 760 magenta black 760 Appendix F How to: Understand and Use Standard Libraries { cout << setw(FIELD_SIZE) << "type" << setw(FIELD_SIZE) << "low" << setw(FIELD_SIZE) << "high" << endl << endl; Print("char", CHAR_MIN, CHAR_MAX); Print("uchar", 0, UCHAR_MAX); Print("short", SHRT_MIN, SHRT_MAX); Print("ushort",0, USHRT_MAX); Print("int", INT_MIN, INT_MAX); Print("uint", 0, UINT_MAX); Print("long", LONG_MIN, LONG_MAX); Print("ulong", 0, ULONG_MAX); return 0; } void Print(const string& type, long int low, unsigned long int high) // postcondition: values printed in field width FIELD_SIZE { cout << setw(FIELD_SIZE) << type << setw(FIELD_SIZE) << low << setw(FIELD_SIZE) << high << endl; } oldlimits.cpp OUTPUT prompt> oldlimits type low high char -128 127 uchar 0 255 short -32768 32767 ushort 0 65535 int -2147483648 2147483647 uint 0 4294967295 long -2147483648 2147483647 ulong 0 4294967295 F.2.2 Double Limits in <cfloat> The header file <cfloat> (or <float.h>) imports several constants including DBL_MIN and DBL_MAX which specify the minimal and maximal double values, respectively. June 7, 1999 10:10 owltex Sheet number 26 Page number 761 magenta black F.2 Constants and Limits 761 F.2.3 Limits in <limits> The header file <limits> imports a templated class numeric_limits that provides values related to all the built-in types. Clients can create versions of numeric_limits for programmer-defined classes. For example, we could create a version for the class BigInt. All the methods and constants in numeric_limits are static, so no vari- ables of type numeric_limits are created. We use only four of the methods available in the class numeric_limits. There are many more, for example, in the class numeric_limits<double> specifically for floating point values. In the function printLimits we use the standard C++ operator typeid, imported from <typeinfo>. Basically, typeid allows types to be compared for equality, and provides access to a string form of a type’s name. For more information on numeric_limits and typeid see [Str97]. Program F.2 limits.cpp #include <iostream> #include <limits> #include <typeinfo> #include <iomanip> using namespace std; // print class-specific limits using numeric_limits from <limits> template <class Type> void printLimits(const Type& t) // post: print max,min values and # bits used by t { cout << "\ninformation for " << typeid(t).name() << endl; cout << "min =\t" << numeric_limits<Type>::min() << endl; cout << "max =\t" << numeric_limits<Type>::max() << endl; cout << "#bits=\t" << numeric_limits<Type>::digits << endl; cout << "is integral? " << boolalpha << numeric_limits<Type>::is_integer << endl; } int main() { printLimits(0); printLimits(0u); printLimits(0L); printLimits('a'); printLimits(static_cast<unsigned char>('a')); printLimits(0.0); printLimits(static_cast<float>(0.0)); return 0; } limits.cpp June 7, 1999 10:10 owltex Sheet number 27 Page number 762 magenta black 762 Appendix F How to: Understand and Use Standard Libraries OUTPUT prompt> limits information for int min = -2147483648 max = 2147483647 #bits= 31 is integral? true information for unsigned int min = 0 max = 4294967295 #bits= 32 is integral? true information for long min = -2147483648 max = 2147483647 #bits= 31 is integral? true information for char min = -128 actually prints a char, not an int max = 128 actually prints a char, not an int #bits= 7 is integral? true information for unsigned char min = 0 max = 255 #bits= 8 is integral? true information for double min = 2.22507e-308 max = 1.79769e+308 #bits= 53 # bits in mantissa is integral? false information for float min = 1.17549e-38 max = 3.40282e+38 #bits= 24 # bits in mantissa is integral? false June 7, 1999 10:10 owltex Sheet number 28 Page number 763 magenta black F.2 Constants and Limits 763 F.2.4 ASCIIValues Since most C++ environments use ASCII coding for characters, Table F.3 provides ASCII values for all the standard characters.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages11 Page
-
File Size-