home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / CLASSES.ZIP / CP_UTIL.CPP < prev   
C/C++ Source or Header  |  1990-02-11  |  2KB  |  103 lines

  1. //  Module:     Cp_Util
  2. //  Version:    2.20
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    Any
  6. //
  7. //  Purpose:    Utility methods for Complex class
  8. //
  9. //  Written by: Scott Robert Ladd
  10.  
  11. #include "Complex.hpp"
  12.  
  13. extern "C"
  14.     {
  15.     #include "math.h"
  16.     #include "stdio.h"
  17.     #include "stdlib.h"
  18.     }
  19.  
  20. // prototype for default error handler
  21. static void DefaultHandler();
  22.  
  23. // assignment of default handler address to error function pointer
  24. void (* Complex::ErrorHandler)() = DefaultHandler;
  25.  
  26. // default error handler
  27. static void DefaultHandler()
  28.     {
  29.     puts("\aERROR in complex object: DIVIDE BY ZERO\n");
  30.     exit(1);
  31.     }
  32.  
  33. // constructors
  34. Complex::Complex (void)
  35.     {
  36.     Real = 0.0;
  37.     Imag = 0.0;
  38.     }
  39.  
  40. Complex::Complex (const Complex & c)
  41.     {
  42.     Real = c.Real;
  43.     Imag = c.Imag;
  44.     }
  45.  
  46. Complex::Complex (const double & r, const double & i)
  47.     {
  48.     Real = r;
  49.     Imag = i;
  50.     }
  51.  
  52. Complex::Complex (const double & r)
  53.     {
  54.     Real = r;
  55.     Imag = 0.0;
  56.     }
  57.  
  58. // method to set error handler function
  59. void Complex::SetErrorHandler(void (* userHandler)())
  60.     {
  61.     ErrorHandler = userHandler;
  62.     }
  63.  
  64. // value extraction methods
  65. double real (const Complex & c)
  66.     {
  67.     return c.Real;
  68.     }
  69.  
  70. double imag (const Complex & c)
  71.     {
  72.     return c.Imag;
  73.     }
  74.  
  75. // assignment method
  76. void Complex::operator = (const Complex & c)
  77.     {
  78.     Real = c.Real;
  79.     Imag = c.Imag;
  80.     }
  81.  
  82. // utility methods
  83. double abs(const Complex & c)
  84.     {
  85.     double result = sqrt(c.Real * c.Real + c.Imag * c.Imag);
  86.  
  87.     return result;
  88.     }
  89.  
  90. double norm(const Complex & c)
  91.     {
  92.     double result = (c.Real * c.Real) + (c.Imag * c.Imag);
  93.  
  94.     return result;
  95.     }
  96.  
  97. double arg(const Complex & c)
  98.     {
  99.     double result = atan2(c.Imag, c.Real);
  100.  
  101.     return result;
  102.     }
  103.