epoc32/include/tools/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
williamr@4
    22
#  include <stl/_complex.h>
williamr@4
    23
#endif
williamr@4
    24
williamr@4
    25
#if !defined (_STLP_USE_NO_IOSTREAMS)
williamr@4
    26
#  ifndef _STLP_INTERNAL_ISTREAM
williamr@4
    27
#    include <stl/_istream.h>
williamr@4
    28
#  endif
williamr@4
    29
williamr@4
    30
#  ifndef _STLP_INTERNAL_SSTREAM
williamr@4
    31
#    include <stl/_sstream.h>
williamr@4
    32
#  endif
williamr@4
    33
williamr@4
    34
#  ifndef _STLP_STRING_IO_H
williamr@4
    35
#    include <stl/_string_io.h>
williamr@4
    36
#  endif
williamr@4
    37
#endif
williamr@4
    38
williamr@4
    39
_STLP_BEGIN_NAMESPACE
williamr@4
    40
williamr@4
    41
// Non-inline member functions.
williamr@4
    42
williamr@4
    43
template <class _Tp>
williamr@4
    44
void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
williamr@4
    45
                        const _Tp& __z2_r, const _Tp& __z2_i,
williamr@4
    46
                        _Tp& __res_r, _Tp& __res_i) {
williamr@4
    47
  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
williamr@4
    48
  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
williamr@4
    49
williamr@4
    50
  if (__ar <= __ai) {
williamr@4
    51
    _Tp __ratio = __z2_r / __z2_i;
williamr@4
    52
    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
williamr@4
    53
    __res_r = (__z1_r * __ratio + __z1_i) / __denom;
williamr@4
    54
    __res_i = (__z1_i * __ratio - __z1_r) / __denom;
williamr@4
    55
  }
williamr@4
    56
  else {
williamr@4
    57
    _Tp __ratio = __z2_i / __z2_r;
williamr@4
    58
    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
williamr@4
    59
    __res_r = (__z1_r + __z1_i * __ratio) / __denom;
williamr@4
    60
    __res_i = (__z1_i - __z1_r * __ratio) / __denom;
williamr@4
    61
  }
williamr@4
    62
}
williamr@4
    63
williamr@4
    64
template <class _Tp>
williamr@4
    65
void complex<_Tp>::_div(const _Tp& __z1_r,
williamr@4
    66
                        const _Tp& __z2_r, const _Tp& __z2_i,
williamr@4
    67
                        _Tp& __res_r, _Tp& __res_i) {
williamr@4
    68
  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
williamr@4
    69
  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
williamr@4
    70
williamr@4
    71
  if (__ar <= __ai) {
williamr@4
    72
    _Tp __ratio = __z2_r / __z2_i;
williamr@4
    73
    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
williamr@4
    74
    __res_r = (__z1_r * __ratio) / __denom;
williamr@4
    75
    __res_i = - __z1_r / __denom;
williamr@4
    76
  }
williamr@4
    77
  else {
williamr@4
    78
    _Tp __ratio = __z2_i / __z2_r;
williamr@4
    79
    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
williamr@4
    80
    __res_r = __z1_r / __denom;
williamr@4
    81
    __res_i = - (__z1_r * __ratio) / __denom;
williamr@4
    82
  }
williamr@4
    83
}
williamr@4
    84
williamr@4
    85
// I/O.
williamr@4
    86
#if !defined (_STLP_USE_NO_IOSTREAMS)
williamr@4
    87
williamr@4
    88
// Complex output, in the form (re,im).  We use a two-step process
williamr@4
    89
// involving stringstream so that we get the padding right.
williamr@4
    90
template <class _Tp, class _CharT, class _Traits>
williamr@4
    91
basic_ostream<_CharT, _Traits>& _STLP_CALL
williamr@4
    92
operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z) {
williamr@4
    93
  basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
williamr@4
    94
  __tmp.flags(__os.flags());
williamr@4
    95
  __tmp.imbue(__os.getloc());
williamr@4
    96
  __tmp.precision(__os.precision());
williamr@4
    97
  __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
williamr@4
    98
  return __os << __tmp.str();
williamr@4
    99
}
williamr@4
   100
williamr@4
   101
// Complex input from arbitrary streams.  Note that results in some
williamr@4
   102
// locales may be confusing, since the decimal character varies with
williamr@4
   103
// locale and the separator between real and imaginary parts does not.
williamr@4
   104
williamr@4
   105
template <class _Tp, class _CharT, class _Traits>
williamr@4
   106
basic_istream<_CharT, _Traits>& _STLP_CALL
williamr@4
   107
operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z) {
williamr@4
   108
  _Tp  __re = 0;
williamr@4
   109
  _Tp  __im = 0;
williamr@4
   110
williamr@4
   111
  // typedef ctype<_CharT> _Ctype;
williamr@4
   112
  //  locale __loc = __is.getloc();
williamr@4
   113
  //const _Ctype&  __c_type  = use_facet<_Ctype>(__loc);
williamr@4
   114
  const ctype<_CharT>& __c_type = *__STATIC_CAST(const ctype<_CharT>*, __is._M_ctype_facet());
williamr@4
   115
williamr@4
   116
  const char __punct[4] = "(,)";
williamr@4
   117
  _CharT __wpunct[3];
williamr@4
   118
  __c_type.widen(__punct, __punct + 3, __wpunct);
williamr@4
   119
williamr@4
   120
  _CharT __c;
williamr@4
   121
williamr@4
   122
  __is >> __c;
williamr@4
   123
  if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
williamr@4
   124
    __is >> __re >> __c;
williamr@4
   125
    if (_Traits::eq(__c, __wpunct[1]))  // Comma
williamr@4
   126
      __is >> __im >> __c;
williamr@4
   127
    if (!_Traits::eq(__c, __wpunct[2])) // Right paren
williamr@4
   128
      __is.setstate(ios_base::failbit);
williamr@4
   129
  }
williamr@4
   130
  else {
williamr@4
   131
    __is.putback(__c);
williamr@4
   132
    __is >> __re;
williamr@4
   133
  }
williamr@4
   134
williamr@4
   135
  if (__is)
williamr@4
   136
    __z = complex<_Tp>(__re, __im);
williamr@4
   137
  return __is;
williamr@4
   138
}
williamr@4
   139
williamr@4
   140
#endif /* _STLP_USE_NO_IOSTREAMS */
williamr@4
   141
williamr@4
   142
_STLP_END_NAMESPACE
williamr@4
   143
williamr@4
   144
#endif /* _STLP_COMPLEX_C */
williamr@4
   145
williamr@4
   146
// Local Variables:
williamr@4
   147
// mode:C++
williamr@4
   148
// End: