home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / ZCOMPLEX.ZIP / COMPLEX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-17  |  2.7 KB  |  137 lines

  1.  
  2. //    Module:        Complex
  3. //    Version:    2.00  28-Oct-1989
  4. //    Language:    C++ 2.0;  Environ: Any;  Compilers: Zortech C++ 2.01
  5. //    Purpose:    Provides the class "Complex" for C++ programs.  The
  6. //            majority of the class is implemented inline for
  7. //            efficiency.  Only the division, power, and i/o methods
  8. //            are actual functions.
  9. //    Written by:    Scott Robert Ladd, 705 West Virginia, Gunnison CO 81230
  10. //            BBS (303)641-6438; FidoNet 1:104/708
  11.  
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <stream.hpp>
  15. #include "complex.hpp"
  16.  
  17. static void    defaultHandler ();
  18. void ( * Complex::errorHandler) () = defaultHandler;
  19.  
  20. static
  21. void
  22. defaultHandler (
  23. ) {
  24.     cout << "\aERROR in complex object: DIVIDE BY ZERO\n";
  25.     exit ( 1);
  26. }
  27.  
  28. Complex
  29. operator/ (
  30.     const Complex &    c1,
  31.     const Complex &    c2
  32. ) {
  33.     Complex        result;
  34.     double        den;
  35.     den = norm ( c2);
  36.     if ( den != 0.0) {
  37.         result.re = ( c1.re * c2.re + c1.im * c2.im) / den;
  38.         result.im = ( c1.im * c2.re - c1.re * c2.im) / den;
  39.     } else
  40.         Complex::errorHandler ();
  41.     return result;
  42. }
  43.  
  44. Complex
  45. Complex::operator/= (
  46.     const Complex &    c
  47. ) {
  48.     double        den;
  49.     den = norm ( c);
  50.     if ( den != 0.0) {
  51.         double        holdr = re;
  52.         re = ( re * c.re + im * c.im) / den;
  53.         im = ( im * c.re - holdr * c.im) / den;
  54.     } else
  55.         Complex::errorHandler ();
  56.     return *this;
  57. }
  58.  
  59. Complex
  60. pow (
  61.     const Complex &    base,
  62.     const Complex &    power
  63. ) {
  64.     Complex        result;
  65.     if ( power.re == 0.0 && power.im == 0.0) {
  66.         result.re = 1.0;
  67.         result.im = 0.0;
  68.     } else
  69.         if ( base.re != 0.0 || base.im != 0.0)
  70.             result = exp ( log ( base) * power);
  71.         else
  72.             Complex::errorHandler ();
  73.     return result;
  74. }
  75.  
  76. Complex
  77. sqrt (
  78.     const Complex &    c
  79. ) {
  80.     Complex        result;
  81.     double        r, i, ratio, w;
  82.     if ( c.re != 0.0 || c.im != 0.0) {
  83.         r = ( c.re < 0.0 ? -c.re : c.re);
  84.         i = ( c.im < 0.0 ? -c.im : c.im);
  85.         if ( r > i) {
  86.             ratio = i / r;
  87.             w = sqrt ( r) * sqrt ( 0.5 * ( 1.0 +
  88.                 sqrt ( 1.0 + ratio * ratio)));
  89.         } else {
  90.             ratio = r / i;
  91.             w = sqrt ( i) * sqrt ( 0.5 * ( ratio +
  92.                 sqrt ( 1.0 + ratio * ratio)));
  93.         }
  94.         if ( c.re > 0.0) {
  95.             result.re = w;
  96.             result.im = c.im / ( 2.0 * w);
  97.         } else {
  98.             result.im = ( c.im > 0.0 ? w : -w);
  99.             result.re = c.im / ( 2.0 * result.im);
  100.         }
  101.     } else
  102.         Complex::errorHandler ();
  103.     return result;
  104. }
  105.  
  106. ostream &
  107. operator<< (
  108.     ostream &    os,
  109.     const Complex &    c
  110. ) {
  111.     os << form ( "(%+1g%+1gi)", c.re, c.im);
  112.     return os;
  113. }
  114.  
  115. istream &
  116. operator>> (
  117.     istream &    is,
  118.     Complex &    c
  119. ) {
  120.     char        ch;
  121.     c.re = 0.0;
  122.     c.im = 0.0;
  123.     is >> ch;
  124.     if ( ch == '(') {
  125.         is >> c.re >> ch;
  126.         if ( ch == ',')
  127.             is >> c.im >> ch;
  128.         if ( ch != ')')
  129.             is.clear ( _bad);
  130.     } else {
  131.         is.putback ( ch);
  132.         is >> c.re;
  133.     }
  134.     return is;
  135. }
  136.  
  137.