C++ Toolkit for Engineers and Scientists Second Edition Springer Science+Business Media, LLC Ja mes T. Smith C++ Toolkit for Engineers and Scientists Second Edition With 12 Illustrations i Springer James T. Smith Department of Mathematics San Francisco State University 1600 Holloway A venue San Francisco, CA 94132-1722 USA [email protected] Windows 95, 98, NT are registered trademarks of the Microsoft Corporation. Intel486 is a registered trademark of Intel. Library of Congress Cataloging-in-Publication Data Smith, James T., 1939- C++ toolkit for engineers and scientists/James T. Smith. p. cm. Includes bibliographical references. Additional material ta this book can be downloded fram http://extras.springer.com. 1. C++ (Computer program language) 1. Title. QA76.73.CI53S654 1999 005.13'3~c21 99-18595 Printed on acid-free paper. ISBN 978-0-387-98797-2 ISBN 978-1-4612-1474-8 (eBook) DOI 10.1007/978-1-4612-1474-8 © 1999 Springer Science+Business Media New York Originally published by Springer-Verlag New York Inc. in 1999 All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+B usiness Media, LLC), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or-hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone. Production managed by Frank McGuckin; manufacturing supervised by Joe Quatela. Camera-ready copy provided by the author. 987654321 Contents Preface XIII About the Author xv 1 Introduction 1 1.1 WHO NEEDS THIS BOOK? 2 Prerequisites 4 1.2 WORKING WITH SOFTWARE TOOLS 5 1.3 THIS BOOK'S STRUCTURE 6 What Would Come Next? 8 The Accompanying and Optional Diskettes 8 MSP Revisions and Extensions 9 1.4 C, C++, AND BORLAND C++ 9 Why Use C++? 9 Is C+ + a High-Level Language? 10 Software Tools 12 Minor Considerations 12 Borland C+ + 13 1.5 THIS BOOK'S MSP SOFTWARE TOOLKIT 14 1.6 REFERENCES AND FURTHER TOPICS 16 v vi c++ Toolkit for Engineers and Scientists 2 Software Organization 21 2.1 SOFTWARE LAYERS 21 Basic Input/Output System (BIOS) 22 DOS/Windows Opemting System 24 Borland C++ Libmry and Run-Time Support 26 Mathematical Software Package (MSP) 27 Client Programs 28 2.2 C+ + MINOR FEATURES 28 / / Comments 29 Local Variable Declarations 29 new and delete Opemtors 29 Mandatory Function Prototypes 30 Initialized Parameters 30 Reference Parameters 30 C++ Version 2 Stream Input/Output 31 2.3 OVERLOADING 33 Function Templates 34 Using Templates for Libmry Code 35 2.4 ABSTRACT DATA TYPES AND CLASSES 36 Abstract Data Types (ADTs) 37 Parallel with Higher Mathematics 38 C+ + Classes and Objects 38 Class Templates 39 Member Functions 40 2.5 MEMBER AND FRIEND FUNCTIONS 40 Principal Argument Syntax 41 Selectors 41 Opemtor Functions 42 Membership Criteria and Friend Functions 42 2.6 CONSTRUCTORS AND DESTRUCTORS 43 Default Constructor 43 Copy Constructor 44 Converters 45 Assignment Operator 45 Destructors 46 2.7 DERIVED CLASSES 46 Contents vii 2.8 EXCEPTIONS 48 The Stack and Function Invocation 49 The Stack and Exception Handling 50 3 Computing with Scalars 55 3.1 MSP General MODULE 57 #ifndef and #endif 58 Definition of This 58 Identifier Capitalization 59 The Boolean Type 60 Empty Strings and Blank Characters 61 Pausing to Inspect Output 62 The Digits Function 63 max and min 63 Templates for Derived Functions and Operators 64 Exceptions 64 3.2 INTEGER ARITHMETIC 65 C+ + Integer Types 65 Addition, Subtraction, and Multiplication 66 Order and Absolute Value 67 Integer Division 68 Additional Integer Operations 69 Formatting Output 70 Integers of Arbitrary Length 70 3.3 FLOATING-POINT ARITHMETIC 70 Real and Floating-Point Numbers 71 IEEE Binary Floating-Point Standard 72 Arithmetic and Rounding 74 Input/Output 76 3.4 Math. H LIBRARY FUNCTIONS 76 Real Arithmetic Functions 77 Real Powers, Logarithms, and Hyperbolic Functions 78 Real Trigonometric Functions 79 3.5 Complex. H LIBRARY FUNCTIONS 81 The complex Class 81 complex Arithmetic 82 Complex. H Elementary Functions 83 viii c++ Toolkit for Engineers and Scientists Exponential, Hyperbolic and Trigonometric Functions 84 Square Roots, Logarithms, and Powers 86 Inverse Trigonometric Functions 88 Need for complex (z) Casts 88 3.6 TESTING LIBRARY FUNCTIONS 89 Internal Validation Experiments 90 3.7 HANDLING Math.H EXCEPTIONS 93 Math. H Error Handling 93 MSP Function _matherr 95 Complex. H Errors 98 3.8 HANDLING FLOATING-POINT EXCEPTIONS 99 Interrupts 99 Hardware Exceptions 101 Coprocessor Exception Masking 102 Borland C+ + Initialization and Exit Routine 104 Borland C++ Hardware Exception Handling 104 Signaling 105 MSP SIGFPE Service Function FPError 107 3.9 MSP Scalar MODULE 110 Codas for Generating Function Template Instances 112 Type Identification Functions 113 Displaying Scalars 114 Constant ii = complex (0,1 ) 116 abs, real, imag, and conj for Noncomplex Scalars 116 Random Scalars 117 3.10 MSP ElemFunc MODULE 117 Factorials and Binomial Coefficients 118 Functions Root and logbase 118 Hyperbolic Functions 119 Trigonometric Functions 120 4 Solving Scalar Equations 123 4.1 PRELIMINARIES 124 Equate1 Module and Header File 124 Trace Output 124 Bracketing Intervals 125 Checking Solutions 125 Contents ix 4.2 BISECTION METHOD 126 4.3 FIXPOINT ITERATION 133 4.4 NEWTON-RAPHSON ITERATION 139 5 Vector Classes 151 5.1 Vector MODULE INFRASTRUCTURE 152 The Vector Data Structure 152 Selectors 154 Constructors 1 5 5 Destructor 158 Copiers 159 Assignment 160 Equality 163 Keyboard Input and Display Output 163 Stream Input/Output 166 Coda 169 5.2 VECTOR ALGEBRA FUNCTIONS 170 Constructing Special Vectors 171 Vector Norms 173 Plus, Minus, and Conjugate 174 Addition and Subtraction 175 Scalar Multiplication 177 Scalar and Inner Products 177 Cross and Triple Products 179 Replacement Operators 180 5.3 Vector MODULE DEMONSTRATION 180 Contours and Surfaces 180 Stokes' Theorem 186 6 Polynomial Classes 189 6.1 Polynom MODULE INFRASTRUCTURE 190 The Degree Selector 192 Constructors 193 Copiers, Assignment, and Type Conversion 195 x c++ Toolkit for Engineers and Scientists 6.2 POLYNOMIAL ALGEBRA FUNCTIONS 196 Negation, Addition, and Subtraction 197 Multiplication and Powers 199 Division 202 Differentiation and Integration 206 Horner's Algorithm and Evaluation 207 Testing with Legendre polynomials 210 6.3 POLYNOMIAL ROOTS 212 Implementing Newton-Raphson Iteration 213 Cauchy's Bound 214 Multiplicity of a Root 215 Finding Real Roots 218 Finding All Real Roots 219 Testing with Legendre Polynomials 222 Finding Complex Roots 223 7 Matrix Classes 229 7.1 Matrix MODULE INFRASTRUCTURE 230 The Matrix Data Structure 230 Selectors 232 Constructors 234 Destructor 236 Converters 237 Copiers 238 Assignment 240 Equality 241 Keyboard Input and Display Output 241 Stream Input/Output 243 7.2 MATRIX ALGEBRA FUNCTIONS 245 Constructing Special Matrices 248 Matrix Norms 248 Plus and Minus 250 Conjugate and Transpose 250 Addition and Subtraction, Trace 252 Scalar Multiplication 253 Matrix Multiplication 253 Replacement Operators 255