Debian Codegraph @

doc/examples/fixed-class.cpp

raw
1 #include <blitz/array.h>
2 #include <blitz/numinquire.h> // for huge()
3
4 using namespace blitz;
5
6 // A simple fixed point arithmetic class which represents a point
7 // in the interval [0,1].
8 class FixedPoint {
9
10 public:
11 typedef unsigned int T_mantissa;
12
13 FixedPoint() { }
14
15 FixedPoint(T_mantissa mantissa)
16 {
17 mantissa_ = mantissa;
18 }
19
20 FixedPoint(double value)
21 {
22 assert((value >= 0.0) && (value <= 1.0));
23 mantissa_ = static_cast<T_mantissa>(value * huge(T_mantissa()));
24 }
25
26 FixedPoint operator+(FixedPoint x)
27 { return FixedPoint(mantissa_ + x.mantissa_); }
28
29 double value() const
30 { return mantissa_ / double(huge(T_mantissa())); }
31
32 private:
33 T_mantissa mantissa_;
34 };
35
36 ostream& operator<<(ostream& os, const FixedPoint& a)
37 {
38 os << a.value();
39 return os;
40 }
41
42 int main()
43 {
44 // Create an array using the FixedPoint class:
45
46 Array<FixedPoint, 2> A(4,4), B(4,4);
47
48 A = 0.5, 0.3, 0.8, 0.2,
49 0.1, 0.3, 0.2, 0.9,
50 0.0, 1.0, 0.7, 0.4,
51 0.2, 0.3, 0.8, 0.4;
52
53 B = A + 0.05;
54
55 cout << "B = " << B << endl;
56
57 return 0;
58 }