_complex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_COMPLEX_C
  19. #define _STLP_COMPLEX_C
  20. #ifndef _STLP_INTERNAL_COMPLEX
  21. # include <stl/_complex.h>
  22. #endif
  23. #if !defined (_STLP_USE_NO_IOSTREAMS)
  24. # ifndef _STLP_INTERNAL_ISTREAM
  25. # include <stl/_istream.h>
  26. # endif
  27. # ifndef _STLP_INTERNAL_SSTREAM
  28. # include <stl/_sstream.h>
  29. # endif
  30. # ifndef _STLP_STRING_IO_H
  31. # include <stl/_string_io.h>
  32. # endif
  33. #endif
  34. _STLP_BEGIN_NAMESPACE
  35. // Non-inline member functions.
  36. template <class _Tp>
  37. void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
  38. const _Tp& __z2_r, const _Tp& __z2_i,
  39. _Tp& __res_r, _Tp& __res_i) {
  40. _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
  41. _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
  42. if (__ar <= __ai) {
  43. _Tp __ratio = __z2_r / __z2_i;
  44. _Tp __denom = __z2_i * (1 + __ratio * __ratio);
  45. __res_r = (__z1_r * __ratio + __z1_i) / __denom;
  46. __res_i = (__z1_i * __ratio - __z1_r) / __denom;
  47. }
  48. else {
  49. _Tp __ratio = __z2_i / __z2_r;
  50. _Tp __denom = __z2_r * (1 + __ratio * __ratio);
  51. __res_r = (__z1_r + __z1_i * __ratio) / __denom;
  52. __res_i = (__z1_i - __z1_r * __ratio) / __denom;
  53. }
  54. }
  55. template <class _Tp>
  56. void complex<_Tp>::_div(const _Tp& __z1_r,
  57. const _Tp& __z2_r, const _Tp& __z2_i,
  58. _Tp& __res_r, _Tp& __res_i) {
  59. _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
  60. _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
  61. if (__ar <= __ai) {
  62. _Tp __ratio = __z2_r / __z2_i;
  63. _Tp __denom = __z2_i * (1 + __ratio * __ratio);
  64. __res_r = (__z1_r * __ratio) / __denom;
  65. __res_i = - __z1_r / __denom;
  66. }
  67. else {
  68. _Tp __ratio = __z2_i / __z2_r;
  69. _Tp __denom = __z2_r * (1 + __ratio * __ratio);
  70. __res_r = __z1_r / __denom;
  71. __res_i = - (__z1_r * __ratio) / __denom;
  72. }
  73. }
  74. // I/O.
  75. #if !defined (_STLP_USE_NO_IOSTREAMS)
  76. // Complex output, in the form (re,im). We use a two-step process
  77. // involving stringstream so that we get the padding right.
  78. template <class _Tp, class _CharT, class _Traits>
  79. basic_ostream<_CharT, _Traits>& _STLP_CALL
  80. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z) {
  81. basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
  82. __tmp.flags(__os.flags());
  83. __tmp.imbue(__os.getloc());
  84. __tmp.precision(__os.precision());
  85. __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
  86. return __os << __tmp.str();
  87. }
  88. // Complex input from arbitrary streams. Note that results in some
  89. // locales may be confusing, since the decimal character varies with
  90. // locale and the separator between real and imaginary parts does not.
  91. template <class _Tp, class _CharT, class _Traits>
  92. basic_istream<_CharT, _Traits>& _STLP_CALL
  93. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z) {
  94. _Tp __re = 0;
  95. _Tp __im = 0;
  96. const ctype<_CharT>& __c_type = *__is._M_ctype_facet();
  97. const char __punct[4] = "(,)";
  98. _CharT __wpunct[3];
  99. __c_type.widen(__punct, __punct + 3, __wpunct);
  100. _CharT __c;
  101. __is >> __c;
  102. if (_Traits::eq(__c, __wpunct[0])) { // Left paren
  103. __is >> __re >> __c;
  104. if (_Traits::eq(__c, __wpunct[1])) // Comma
  105. __is >> __im >> __c;
  106. if (!_Traits::eq(__c, __wpunct[2])) // Right paren
  107. __is.setstate(ios_base::failbit);
  108. }
  109. else {
  110. __is.putback(__c);
  111. __is >> __re;
  112. }
  113. if (__is)
  114. __z = complex<_Tp>(__re, __im);
  115. return __is;
  116. }
  117. #endif /* _STLP_USE_NO_IOSTREAMS */
  118. _STLP_END_NAMESPACE
  119. #endif /* _STLP_COMPLEX_C */
  120. // Local Variables:
  121. // mode:C++
  122. // End: