epoc32/include/stdapis/stlport/stl/_complex.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
 * Copyright (c) 1999
williamr@4
     3
 * Silicon Graphics Computer Systems, Inc.
williamr@4
     4
 *
williamr@4
     5
 * Copyright (c) 1999 
williamr@4
     6
 * Boris Fomitchev
williamr@4
     7
 *
williamr@4
     8
 * This material is provided "as is", with absolutely no warranty expressed
williamr@4
     9
 * or implied. Any use is at your own risk.
williamr@4
    10
 *
williamr@4
    11
 * Permission to use or copy this software for any purpose is hereby granted 
williamr@4
    12
 * without fee, provided the above notices are retained on all copies.
williamr@4
    13
 * Permission to modify the code and to distribute modified code is granted,
williamr@4
    14
 * provided the above notices are retained, and a notice that the code was
williamr@4
    15
 * modified is included with the above copyright notice.
williamr@4
    16
 *
williamr@4
    17
 */ 
williamr@4
    18
#ifndef _STLP_COMPLEX_C
williamr@4
    19
#define _STLP_COMPLEX_C
williamr@4
    20
williamr@4
    21
# ifndef _STLP_internal_complex_h
williamr@4
    22
#  include <stl/_complex.h>
williamr@4
    23
# endif
williamr@4
    24
williamr@4
    25
#include <istream>
williamr@4
    26
williamr@4
    27
#ifdef _STLP_USE_NEW_IOSTREAMS
williamr@4
    28
# include <sstream>
williamr@4
    29
#endif
williamr@4
    30
williamr@4
    31
_STLP_BEGIN_NAMESPACE
williamr@4
    32
williamr@4
    33
// Non-inline member functions.
williamr@4
    34
williamr@4
    35
template <class _Tp>
williamr@4
    36
void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
williamr@4
    37
                        const _Tp& __z2_r, const _Tp& __z2_i,
williamr@4
    38
                        _Tp& __res_r, _Tp& __res_i) {
williamr@4
    39
  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
williamr@4
    40
  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
williamr@4
    41
williamr@4
    42
  if (__ar <= __ai) {
williamr@4
    43
    _Tp __ratio = __z2_r / __z2_i;
williamr@4
    44
    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
williamr@4
    45
    __res_r = (__z1_r * __ratio + __z1_i) / __denom;
williamr@4
    46
    __res_i = (__z1_i * __ratio - __z1_r) / __denom;
williamr@4
    47
  }
williamr@4
    48
  else {
williamr@4
    49
    _Tp __ratio = __z2_i / __z2_r;
williamr@4
    50
    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
williamr@4
    51
    __res_r = (__z1_r + __z1_i * __ratio) / __denom;
williamr@4
    52
    __res_i = (__z1_i - __z1_r * __ratio) / __denom;
williamr@4
    53
  }
williamr@4
    54
}
williamr@4
    55
williamr@4
    56
template <class _Tp>
williamr@4
    57
void complex<_Tp>::_div(const _Tp& __z1_r,
williamr@4
    58
                        const _Tp& __z2_r, const _Tp& __z2_i,
williamr@4
    59
                        _Tp& __res_r, _Tp& __res_i) {
williamr@4
    60
  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
williamr@4
    61
  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
williamr@4
    62
williamr@4
    63
  if (__ar <= __ai) {
williamr@4
    64
    _Tp __ratio = __z2_r / __z2_i;
williamr@4
    65
    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
williamr@4
    66
    __res_r = (__z1_r * __ratio) / __denom;
williamr@4
    67
    __res_i = - __z1_r / __denom;
williamr@4
    68
  }
williamr@4
    69
  else {
williamr@4
    70
    _Tp __ratio = __z2_i / __z2_r;
williamr@4
    71
    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
williamr@4
    72
    __res_r = __z1_r / __denom;
williamr@4
    73
    __res_i = - (__z1_r * __ratio) / __denom;
williamr@4
    74
  }
williamr@4
    75
}
williamr@4
    76
williamr@4
    77
// I/O.
williamr@4
    78
williamr@4
    79
#ifdef _STLP_USE_NEW_IOSTREAMS
williamr@4
    80
williamr@4
    81
// Complex output, in the form (re,im).  We use a two-step process 
williamr@4
    82
// involving stringstream so that we get the padding right.  
williamr@4
    83
template <class _Tp, class _CharT, class _Traits>
williamr@4
    84
basic_ostream<_CharT, _Traits>& _STLP_CALL
williamr@4
    85
operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z)
williamr@4
    86
{
williamr@4
    87
  basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
williamr@4
    88
  __tmp.flags(__os.flags());
williamr@4
    89
  __tmp.imbue(__os.getloc());
williamr@4
    90
  __tmp.precision(__os.precision());
williamr@4
    91
  __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
williamr@4
    92
  return __os << __tmp.str();
williamr@4
    93
}
williamr@4
    94
williamr@4
    95
// Complex input from arbitrary streams.  Note that results in some
williamr@4
    96
// locales may be confusing, since the decimal character varies with
williamr@4
    97
// locale and the separator between real and imaginary parts does not.
williamr@4
    98
williamr@4
    99
template <class _Tp, class _CharT, class _Traits>
williamr@4
   100
basic_istream<_CharT, _Traits>& _STLP_CALL
williamr@4
   101
operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z)
williamr@4
   102
{
williamr@4
   103
  _Tp  __re = 0;
williamr@4
   104
  _Tp  __im = 0;
williamr@4
   105
williamr@4
   106
  // typedef ctype<_CharT> _Ctype;
williamr@4
   107
  //  locale __loc = __is.getloc();
williamr@4
   108
  //const _Ctype&  __c_type  = use_facet<_Ctype>(__loc);
williamr@4
   109
  const ctype<_CharT>& __c_type = *(const ctype<_CharT>*)__is._M_ctype_facet();
williamr@4
   110
williamr@4
   111
  char   __punct[4] = "(,)";
williamr@4
   112
  _CharT __wpunct[3];
williamr@4
   113
  __c_type.widen(__punct, __punct + 3, __wpunct);
williamr@4
   114
williamr@4
   115
  _CharT __c;
williamr@4
   116
williamr@4
   117
  __is >> __c;
williamr@4
   118
  if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
williamr@4
   119
    __is >> __re >> __c;
williamr@4
   120
    if (_Traits::eq(__c, __wpunct[1]))  // Comma
williamr@4
   121
      __is >> __im >> __c;
williamr@4
   122
    if (!_Traits::eq(__c, __wpunct[2])) // Right paren
williamr@4
   123
      __is.setstate(ios_base::failbit);
williamr@4
   124
  }
williamr@4
   125
  else {
williamr@4
   126
    __is.putback(__c);
williamr@4
   127
    __is >> __re;
williamr@4
   128
  }
williamr@4
   129
williamr@4
   130
  if (__is)
williamr@4
   131
    __z = complex<_Tp>(__re, __im);
williamr@4
   132
  return __is;
williamr@4
   133
}
williamr@4
   134
williamr@4
   135
williamr@4
   136
#else /* _STLP_USE_NEW_IOSTREAMS */
williamr@4
   137
williamr@4
   138
template <class _Tp>
williamr@4
   139
ostream& _STLP_CALL operator<<(ostream& s, const complex<_Tp>& __z)
williamr@4
   140
{
williamr@4
   141
  return s << "( " << __z._M_re <<", " << __z._M_im <<")";
williamr@4
   142
}
williamr@4
   143
williamr@4
   144
template <class _Tp>
williamr@4
   145
istream& _STLP_CALL operator>>(istream& s, complex<_Tp>& a)
williamr@4
   146
{
williamr@4
   147
  _Tp re = 0, im = 0;
williamr@4
   148
  char 	c = 0;
williamr@4
   149
williamr@4
   150
  s >> c;
williamr@4
   151
  if (c == '(') {
williamr@4
   152
    s >> re >> c;
williamr@4
   153
    if (c == ',') s >> im >> c;
williamr@4
   154
    if (c != ')') s.clear(ios::badbit);
williamr@4
   155
  }
williamr@4
   156
  else {
williamr@4
   157
    s.putback(c);
williamr@4
   158
    s >> re;
williamr@4
   159
  }
williamr@4
   160
williamr@4
   161
  if (s) a = complex<_Tp>(re, im);
williamr@4
   162
  return s;
williamr@4
   163
}
williamr@4
   164
williamr@4
   165
#endif /* _STLP_USE_NEW_IOSTREAMS */
williamr@4
   166
williamr@4
   167
_STLP_END_NAMESPACE
williamr@4
   168
williamr@4
   169
#endif /* _STLP_COMPLEX_C */