B.cpp
unknown
c_cpp
a year ago
1.7 kB
14
Indexable
#include <cmath> #include <iomanip> std::ostream& operator<<(std::ostream& os, const FixedPoint& p) { os << std::fixed << std::setprecision(2) << static_cast<double>(p.GetRepr()) / 100.0; return os; } FixedPoint::FixedPoint(double val) { double scaledValue = val * kInverseOfScalingFactor; //repr_為實數值的100倍 this->repr_ = static_cast<long long>(std::round(scaledValue)); } FixedPoint::FixedPoint(long long repr) { this->repr_ = std::round(repr); //真實值為此值/100 } long long FixedPoint::GetRepr() const{ return this->repr_; } FixedPoint FixedPoint::operator+(const FixedPoint& rhs) const { long long sum = this->repr_ + rhs.GetRepr(); return FixedPoint(sum); } FixedPoint FixedPoint::operator-(const FixedPoint& rhs) const { long long diff = this->repr_ - rhs.GetRepr(); return FixedPoint(diff); } FixedPoint FixedPoint::operator*(const FixedPoint& rhs) const { long long product = std::round(static_cast<double>(this->repr_ * rhs.GetRepr())/100.0); return FixedPoint(product); } FixedPoint FixedPoint::operator/(const FixedPoint& rhs) const { long long quotient = std::round(static_cast<double>(this->repr_*100.0 / rhs.GetRepr())); return FixedPoint(quotient); } bool FixedPoint::operator<(const FixedPoint& rhs) const { return this->repr_ < rhs.GetRepr(); } bool FixedPoint::operator>(const FixedPoint& rhs) const { return this->repr_ > rhs.GetRepr(); } bool FixedPoint::operator==(const FixedPoint& rhs) const { return this->repr_ == rhs.GetRepr(); } bool FixedPoint::operator!=(const FixedPoint& rhs) const { return this->repr_ != rhs.GetRepr(); }
Editor is loading...
Leave a Comment