Debian Codegraph @

doc/examples/fixed-point.h

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 // The type to use for the mantissa
12 typedef unsigned int T_mantissa;
13
14 FixedPoint() { }
15
16 FixedPoint(T_mantissa mantissa)
17 {
18 mantissa_ = mantissa;
19 }
20
21 FixedPoint(double value)
22 {
23 assert((value >= 0.0) && (value <= 1.0));
24 mantissa_ = static_cast<T_mantissa>(value * huge(T_mantissa()));
25 }
26
27 FixedPoint operator+(FixedPoint x)
28 { return FixedPoint(mantissa_ + x.mantissa_); }
29
30 double value() const
31 { return mantissa_ / double(huge(T_mantissa())); }
32
33 private:
34 T_mantissa mantissa_;
35 };
36
37 ostream& operator<<(ostream& os, const FixedPoint& a)
38 {
39 os << a.value();
40 return os;
41 }
42