📄 fixedp.h
字号:
#ifndef __FIXEDP_H__#define __FIXEDP_H__#include <ostream>#include <cmath>using namespace std;template <int shifts_, typename Int_ = int>class FixedPoint { private: template <bool equal_, bool leftLarge_, int leftShifts_, int rightShifts_> struct ConvertHelper_; template <int leftShifts_, int rightShifts_> struct ConvertHelper_<true, false, leftShifts_, rightShifts_> { static Int_ run(Int_ v) { return v; } }; template <int leftShifts_, int rightShifts_> struct ConvertHelper_<false, true, leftShifts_, rightShifts_> { static Int_ run(Int_ v) { return v << leftShifts_ - rightShifts_; } }; template <int leftShifts_, int rightShifts_> struct ConvertHelper_<false, false, leftShifts_, rightShifts_> { static Int_ run(Int_ v) { return v >> rightShifts_ - leftShifts_; } }; template <int leftShifts_, int rightShifts_> Int_ convert_(Int_ v) const { return ConvertHelper_ <(leftShifts_ == rightShifts_), (leftShifts_ > rightShifts_), leftShifts_, rightShifts_>::run(v); } template <typename T> static Int_ conv(T v) { return static_cast<Int_>(v * (1 << shifts_)); } public: Int_ val; FixedPoint() : val(0) {} FixedPoint(double v) : val(conv<double>(v)){} FixedPoint(float v) : val(conv<float>(v)){} FixedPoint(Int_ v) : val(v << shifts_){} template <int shifts2_> explicit FixedPoint(const FixedPoint<shifts2_, Int_> &v){ val = convert_<shifts_, shifts2_>(v.val); } operator Int_() const{return val;} Int_ get() const{return val >> shifts_;} operator double() const{return (double)val / (1 << shifts_);} operator float() const{return (float)val / (1 << shifts_);} FixedPoint& operator = (const Int_ &v) { val = v << shifts_; return *this; } template <int shifts2_> FixedPoint& operator = (const FixedPoint<shifts2_, Int_> &v) { val = convert_<shifts_, shifts2_>(v.val); return *this; } FixedPoint& operator += (const Int_ &v) { val += v << shifts_; return *this; } template <int shifts2_> FixedPoint& operator += (const FixedPoint<shifts2_, Int_> &v) { val += convert_<shifts_, shifts2_>(v.val); return *this; } FixedPoint& operator -= (const Int_ &v) { val -= v << shifts_; return *this; } template <int shifts2_> FixedPoint& operator -= (const FixedPoint<shifts2_, Int_> &v) { val -= convert_<shifts_, shifts2_>(v.val); return *this; } FixedPoint& operator *= (const Int_ &v) { val *= v; return *this; } template <int shifts2_> FixedPoint& operator *= (const FixedPoint<shifts2_, Int_> &v) { val *= v.val; val >>= shifts2_; return *this; } FixedPoint& operator /= (const Int_ &v) { val /= v; return *this; } template <int shifts2_> FixedPoint& operator /= (const FixedPoint<shifts2_, Int_> &v) { val <<= shifts2_; val /= v.val; return *this; } FixedPoint operator + (const Int_ &rhs) const { FixedPoint ret(*this); ret += rhs; return ret; } friend inline FixedPoint operator + (Int_ lhs, FixedPoint rhs) { return rhs.operator+(lhs); } template <int shifts2_> FixedPoint operator + (const FixedPoint<shifts2_, Int_> &rhs) const{ FixedPoint ret(*this); ret += rhs; return ret; } FixedPoint operator-() const{ FixedPoint ret(*this); return ret * -1; } FixedPoint operator - (const Int_ &rhs) const{ FixedPoint ret(*this); ret -= rhs; return ret; } friend inline FixedPoint operator - (Int_ lhs, FixedPoint rhs) { FixedPoint ret(lhs); ret -= rhs; return ret; } template <int shifts2_> FixedPoint operator - (const FixedPoint<shifts2_, Int_> &rhs) const{ FixedPoint ret(*this); ret -= rhs; return ret; } FixedPoint operator * (const Int_ &rhs) const{ FixedPoint ret(*this); ret *= rhs; return ret; } friend inline FixedPoint operator * (Int_ lhs, FixedPoint rhs){ return rhs.operator * (lhs); } template <int shifts2_> FixedPoint operator * (const FixedPoint<shifts2_, Int_> &rhs) const{ FixedPoint ret(*this); ret *= rhs; return ret; } FixedPoint operator / (const Int_ &rhs) const{ FixedPoint ret(*this); ret /= rhs; return ret; } friend inline FixedPoint operator / (const Int_ &lhs, const FixedPoint &rhs) { FixedPoint ret(lhs); ret /= rhs; return ret; } template <int shifts2_> FixedPoint operator / (const FixedPoint<shifts2_, Int_> &rhs) const{ FixedPoint ret(*this); ret /= rhs; return ret; } bool operator > (const Int_ &rhs) const{ return operator>(FixedPoint(rhs)); } template <int shifts2_> bool operator > (const FixedPoint<shifts2_, Int_> &rhs) const{ return val > convert_<shifts_, shifts2_>(rhs.val); } bool operator < (const Int_ &rhs) const{ return operator<(FixedPoint(rhs)); } template <int shifts2_> bool operator < (const FixedPoint<shifts2_, Int_> &rhs) const{ return val < convert_<shifts_, shifts2_>(rhs.val); } bool operator == (const Int_ &rhs) const{ return operator==(FixedPoint(rhs)); } template <int shifts2_> bool operator == (const FixedPoint<shifts2_, Int_> &rhs) const{ return val == convert_<shifts_, shifts2_>(rhs.val); } inline bool operator != (const Int_ &rhs) const{ return !(operator==(FixedPoint(rhs))); } template <int shifts2_> inline bool operator != (const FixedPoint<shifts2_, Int_> &rhs) const{ return val != convert_<shifts_, shifts2_>(rhs.val); } bool operator <= (const Int_ &rhs) const{ return operator<=(FixedPoint(rhs)); } template <int shifts2_> bool operator <= (const FixedPoint<shifts2_, Int_> &rhs) const{ return val <= convert_<shifts_, shifts2_>(rhs.val); } bool operator >= (const Int_ &rhs) const{ return operator>=(FixedPoint(rhs)); } template <int shifts2_> bool operator >= (const FixedPoint<shifts2_, Int_> &rhs) const{ return val >= convert_<shifts_, shifts2_>(rhs.val); } friend inline FixedPoint pow(FixedPoint v, int loop){ FixedPoint ret(v); for(; loop > 0; loop--){ret *= v;} return v; } friend inline FixedPoint sqrt(FixedPoint v){ return FixedPoint(sqrt((double)v)); } friend ostream &operator<<(ostream &out, const FixedPoint &v){ out << (double)v; return out; }};#endif /* __FIXEDP_H__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -