File: fix_test.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (141 lines) | stat: -rw-r--r-- 5,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*!
 * \file
 * \brief Fixed-point classes test program
 * \author Johan Bergman and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/itfixed.h>
#include <iomanip>

using namespace itpp;
using namespace std;

int main()
{
  // This is a hack for improper rounding under MinGW
  cout.precision(8);

  cout << "Testing declaration, initialization and conversion" << endl;
  cout << "==================================================" << endl;
  int shift(10);       // -64...+63 (0 is default)
  int wordlen(20);     // 1...64 (64 is default)
  e_mode emode(TC);    // TC or US (TC is default)
  o_mode omode(WRAP);  // WRAP or SAT (WRAP is default)
  q_mode qmode(TRN);   // RND or TRN (TRN is default)
  Stat *stat_ptr(0);   // 0 or Stat* value (0 is default)

  cout << "For double and complex<double>:" << endl;
  double real_value(3.14159265358979323846);
  cout << "  real_value = " << real_value << endl;
  complex<double> complex_value(100.0/3.0, 200.0/3.0);
  cout << "  complex_value = " << complex_value << endl;

  cout << "For Fix and CFix:" << endl;
  Fix the_fix(real_value, shift, wordlen, emode, omode, qmode, stat_ptr);
  cout << "  the_fix = " << double(the_fix) << endl;
  CFix the_cfix(complex_value, 0.0, shift, wordlen, emode, omode, qmode,
		stat_ptr);
  cout << "  the_cfix = " << complex<double>(the_cfix) << endl;

  cout << "For Fixed and CFixed:" << endl;
  Fixed<20, TC, WRAP, TRN> the_fixed(real_value, shift, stat_ptr);
  cout << "  the_fixed = " << double(the_fixed) << endl;
  CFixed<20, TC, WRAP, TRN> the_cfixed(complex_value, 0.0, shift, stat_ptr);
  cout << "  the_cfixed = " << complex<double>(the_cfixed) << endl;

  cout << "For Fixed and CFixed declared using a typedef:" << endl;
  fixed20 the_fixed20(real_value, shift, stat_ptr);
  cout << "  the_fixed20 = " << double(the_fixed20) << endl;
  cfixed20 the_cfixed20(complex_value, 0.0, shift, stat_ptr);
  cout << "  the_cfixed20 = " << complex<double>(the_cfixed20) << endl;

  cout << "For Fix and CFix declared using a factory:" << endl;
  Fix the_fix20(FIX20);
  the_fix20.set(real_value, shift);
  cout << "  the_fix20 = " << double(the_fix20) << endl;
  CFix the_cfix20(FIX20);
  the_cfix20.set(complex_value, shift);
  cout << "  the_cfix20 = " << complex<double>(the_cfix20) << endl << endl;

  cout << "Testing Array/Vec/Mat declarations and operations" << endl;
  cout << "=================================================" << endl;
  int vec_length(2);

  cout << "For Vec<Fix> and Vec<CFix>:" << endl;
  fixvec the_fixvec(vec_length, FIX20);
  the_fixvec = Fix(real_value, shift);
  cout << "  the_fixvec = " << to_vec(the_fixvec) << endl;
  cfixvec the_cfixvec(vec_length, FIX20);
  the_cfixvec = CFix(complex_value, 0.0, shift);
  cout << "  the_cfixvec = " << to_cvec(the_cfixvec) << endl;
  cout << "  the_cfixvec + the_fixvec = " << to_cvec(the_cfixvec + the_fixvec)
       << endl;
  cout << "  the_cfixvec - the_fixvec = " << to_cvec(the_cfixvec - the_fixvec)
       << endl;
  cout << "  the_cfixvec * the_fixvec = "
       << complex<double>(the_cfixvec * the_fixvec) << endl;
  cout << "  the_cfixvec / the_fix = " << to_cvec(the_cfixvec / the_fix)
       << endl << endl;

  cout << "Testing functions" << endl;
  cout << "=================" << endl;

  cout << "Function is_fix:" << endl;
  Array<Array<fixvec> > the_array2d_fixvec;
  cout << "  is_fix(the_array2d_fixvec) = " << is_fix(the_array2d_fixvec)
       << endl;

  cout << "Function set_fix:" << endl;
  vec original_float = "0:7";
  fixvec resulting_fix(FIX3);
  set_fix(resulting_fix, original_float, 0);
  cout << "  original_float = " << original_float << " => resulting_fix = "
       << resulting_fix << endl;
  vec resulting_float(FIX3);
  set_fix(resulting_float, original_float, 0);
  cout << "  original_float = " << original_float << " => resulting_float = "
       << resulting_float << endl;

  cout << "Function lshift_fix:" << endl;
  Fix fix_to_be_lshifted(FIX16);
  fix_to_be_lshifted = 77;
  cout << "  before lshift: " << fix_to_be_lshifted << " , rep: "
       << fix_to_be_lshifted.get_re() << endl;
  lshift_fix(fix_to_be_lshifted, 1);
  cout << "  after lshift: " << fix_to_be_lshifted << " , rep: "
       << fix_to_be_lshifted.get_re() << endl;

  cout << "Function rshift_fix:" << endl;
  Fix fix_to_be_rshifted(FIX16);
  fix_to_be_rshifted = Fix(3.14, 8);
  cout << "  before rshift: " << fix_to_be_rshifted << " , rep: "
       << fix_to_be_rshifted.get_re() << endl;
  rshift_fix(fix_to_be_rshifted, 6, RND);
  cout << "  after rshift: " << fix_to_be_rshifted << " , rep: "
       << fix_to_be_rshifted.get_re() << endl;

  return 0;
}