?? fixednumber.h
字號:
#pragma once
// FixedNumber.h:
// a template class which support the operation of basic math of fixed point of data
// author: duan yan jun
// 2008.2
#define FIXED_NUMBER_VERSION "0.0.1"
// modify list
//2008.02.01 - version 0.0.1
// + the basic math operation between int and fixed.
template<int shift>
class FixedNumber
{
friend class FixedNumber;
public:
FixedNumber():data(0){};
~FixedNumber(){};
FixedNumber(int other)
{
#ifdef _DEBUG
long long tmp0;
tmp0 = other;
tmp0 <<= shift;
#endif
data = other<<shift;
ASSERT((long long)data==tmp0);
}
FixedNumber(const FixedNumber& other)
{
data = other.data;
}
template <int shift_other>
FixedNumber(const FixedNumber<shift_other>& other)
{
long long tmp0;
tmp0 = other.data;
tmp0 <<= shift;
tmp0 >>= shift_other;
data = tmp0;
ASSERT((long long)data==tmp0);
}
FixedNumber& operator <<= (int move)
{
#ifdef _DEBUG
tmp0 = data;
tmp0 <<= move;
#endif
data <<= move;
ASSERT((long long)data==tmp0);
}
FixedNumber& operator >>= (int move)
{
#ifdef _DEBUG
tmp0 = data;
tmp0 >>= move;
#endif
data >>= move;
ASSERT((long long)data==tmp0);
}
FixedNumber operator - (void) const
{
FixedNumber ret;
ret.data = -data;
return ret;
}
FixedNumber& operator += (const FixedNumber& other)
{
#ifdef _DEBUG
long long tmp0 = data;
tmp0 += other.data;
#endif
data += other.data;
ASSERT(data==tmp0);
return *this;
}
template <int shift_other>
FixedNumber& operator += (const FixedNumber<shift_other>& other)
{
FixedNumber<shift> a(other);
*this += a;
return *this;
}
FixedNumber& operator -= (const FixedNumber& other)
{
#ifdef _DEBUG
long long tmp0 = data;
tmp0 -= other.data;
#endif
data -= other.data;
ASSERT(data==tmp0);
return *this;
}
template <int shift_other>
FixedNumber& operator -= (const FixedNumber<shift_other>& other)
{
FixedNumber<shift> a(other);
*this -= a;
return *this;
}
template <int shift_other>
FixedNumber& operator *= (const FixedNumber<shift_other>& other)
{
long long tmp0 = data;
tmp0 *= other.data;
tmp0 >>= shift_other;
data = tmp0;
ASSERT((long long)data==tmp0);
return *this;
}
template <int shift_other>
FixedNumber& operator /= (const FixedNumber<shift_other>& other)
{
ASSERT(other.data);
long long tmp0 = data;
tmp0 <<= shift_other;
tmp0 /= other.data;
data = tmp0;
ASSERT((long long)data==tmp0);
return *this;
}
///////////////////////////////////////////////////////////////////////////
FixedNumber operator << (int move) const
{
FixedNumber ret(*this);
ret<<=move;
return ret;
}
FixedNumber operator >> (int move) const
{
FixedNumber ret(*this);
ret>>=move;
return ret;
}
//FixedNumber operator + (const FixedNumber& other) const
//{
// FixedNumber a(*this);
// a += other;
// return a;
//}
template< int shift_other >
FixedNumber operator + (const FixedNumber<shift_other>& other) const
{
FixedNumber a(*this);
a += other;
return a;
}
template< int shift_other >
FixedNumber operator - (const FixedNumber<shift_other>& other) const
{
FixedNumber a(*this);
a -= other;
return a;
}
template< int shift_other >
FixedNumber operator * (const FixedNumber<shift_other>& other) const
{
FixedNumber a(*this);
a *= other;
return a;
}
template< int shift_other >
FixedNumber operator / (const FixedNumber<shift_other>& other) const
{
FixedNumber a(*this);
a /= other;
return a;
}
operator int () const
{
return data>>shift;
}
private:
int data;
//static long long tmp0;
//static long long tmp1;
};
typedef FixedNumber<16> F16;
typedef FixedNumber<12> F12;
typedef FixedNumber<20> F20;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -