|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | +#include <climits> |
| 5 | +#include <cmath> |
| 6 | +#include <cassert> |
| 7 | +//#include <string> |
| 8 | + |
| 9 | +template <int kScale> |
| 10 | +class FixedPoint { |
| 11 | + static_assert(kScale >= 1, "FixedPoint requires kScale >= 1"); |
| 12 | + |
| 13 | + int digits; |
| 14 | + |
| 15 | +public: |
| 16 | + FixedPoint() = default; |
| 17 | + |
| 18 | + FixedPoint(int base, int fract = 0) |
| 19 | + : digits(base * kScale + fract) |
| 20 | + { |
| 21 | + if (!(fract < kScale && fract >= 0)) |
| 22 | + qWarning() << base << fract << kScale; |
| 23 | + assert(fract < kScale && fract >= 0); |
| 24 | + assert(base < INT_MAX / kScale); |
| 25 | + } |
| 26 | + |
| 27 | + FixedPoint(double value) |
| 28 | + : FixedPoint(static_cast<int>(std::floor(value)), |
| 29 | + static_cast<int>((value - std::floor(value)) * kScale)) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + int base() const { |
| 34 | + return digits >= 0 ? digits / kScale : digits / kScale - 1; |
| 35 | + } |
| 36 | + |
| 37 | + int fract() const { |
| 38 | + return digits >= 0 ? digits % kScale : kScale - digits % kScale; |
| 39 | + } |
| 40 | + |
| 41 | + int const &rawDigits() const { |
| 42 | + return digits; |
| 43 | + } |
| 44 | + |
| 45 | + int &rawDigits() { |
| 46 | + return digits; |
| 47 | + } |
| 48 | + |
| 49 | + operator double() const { |
| 50 | + // assert(base() * kScale + fract() == digits); |
| 51 | + return (double)digits / kScale; |
| 52 | + } |
| 53 | + |
| 54 | + FixedPoint &operator+=(FixedPoint const &that) { |
| 55 | + digits += that.digits; |
| 56 | + return *this; |
| 57 | + } |
| 58 | + |
| 59 | + FixedPoint &operator-=(FixedPoint const &that) { |
| 60 | + digits -= that.digits; |
| 61 | + return *this; |
| 62 | + } |
| 63 | + |
| 64 | + FixedPoint &operator*=(FixedPoint const &that) { |
| 65 | + digits *= that.digits; |
| 66 | + digits /= kScale; |
| 67 | + return *this; |
| 68 | + } |
| 69 | + |
| 70 | + FixedPoint &operator/=(FixedPoint const &that) { |
| 71 | + digits *= kScale; |
| 72 | + digits /= that.digits; |
| 73 | + return *this; |
| 74 | + } |
| 75 | + |
| 76 | + FixedPoint operator+(FixedPoint const &that) const { |
| 77 | + FixedPoint tmp = *this; |
| 78 | + tmp += that; |
| 79 | + return tmp; |
| 80 | + } |
| 81 | + |
| 82 | + FixedPoint operator-(FixedPoint const &that) const { |
| 83 | + FixedPoint tmp = *this; |
| 84 | + tmp -= that; |
| 85 | + return tmp; |
| 86 | + } |
| 87 | + |
| 88 | + FixedPoint operator*(FixedPoint const &that) const { |
| 89 | + FixedPoint tmp = *this; |
| 90 | + tmp *= that; |
| 91 | + return tmp; |
| 92 | + } |
| 93 | + |
| 94 | + FixedPoint operator/(FixedPoint const &that) const { |
| 95 | + FixedPoint tmp = *this; |
| 96 | + tmp /= that; |
| 97 | + return tmp; |
| 98 | + } |
| 99 | + |
| 100 | + FixedPoint operator+() const { |
| 101 | + return *this; |
| 102 | + } |
| 103 | + |
| 104 | + FixedPoint operator-() const { |
| 105 | + FixedPoint tmp; |
| 106 | + tmp.digits = -digits; |
| 107 | + return tmp; |
| 108 | + } |
| 109 | + |
| 110 | + /*static FixedPoint from_string(std::string str, bool *ok = nullptr, int strBase = 10) { |
| 111 | + if (str.isEmpty()) { |
| 112 | + if (ok) *ok = false; |
| 113 | + return FixedPoint(); |
| 114 | + } |
| 115 | + int sign = 1; |
| 116 | + if (str.front() == '-' && str.size() >= 2) { |
| 117 | + str = str.substr(1); |
| 118 | + sign = -1; |
| 119 | + } |
| 120 | + if (ok) *ok = true; |
| 121 | + size_t dot = str.find('.'); |
| 122 | + if (dot != -1) { |
| 123 | + int base = dot != 0 ? |
| 124 | + std::stoi(str.substr(0, dot), nullptr, strBase) : 0; |
| 125 | + unsigned int fract = dot + 1 != str.size() ? |
| 126 | + std::stoul(str.substr(dot + 1)) : 0; |
| 127 | + if (kScale == 1) { |
| 128 | + fract = 0; |
| 129 | + } else { |
| 130 | + while (fract && fract < kScale / strBase) { |
| 131 | + fract *= 10; |
| 132 | + } |
| 133 | + if (fract >= kScale) |
| 134 | + fract /= strBase; |
| 135 | + } |
| 136 | + return FixedPoint(sign * base, fract); |
| 137 | + } else { |
| 138 | + int base = std::stoi(str.substr(0, dot), nullptr, strBase); |
| 139 | + return FixedPoint(sign * base); |
| 140 | + } |
| 141 | + }*/ |
| 142 | +}; |
0 commit comments