⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fixednumber.h

📁 一个定点数运算的模板类
💻 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 + -