From 9506293205acd4aaff9be6228ed629818ad4c09b Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 12 Jun 2023 01:15:20 +0000 Subject: [PATCH 1/2] dec to bin --- .../fixed_point/fixed_point.hpp | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/packages/server/computation_container/fixed_point/fixed_point.hpp b/packages/server/computation_container/fixed_point/fixed_point.hpp index d09df3bbe..daba1dd7f 100644 --- a/packages/server/computation_container/fixed_point/fixed_point.hpp +++ b/packages/server/computation_container/fixed_point/fixed_point.hpp @@ -11,41 +11,25 @@ namespace qmpc::Utils { -template -inline static constexpr T mypow(T a_, U n_) noexcept -{ - T a = a_; - U n = n_; - T ret = 1; - while (n > 0) - { - if (n & 1) ret *= a; - a *= a; - n >>= 1; - } - return ret; -} namespace mp = boost::multiprecision; using cpp_dec_float = mp::number>; /* T:保持する整数型 D:変換する浮動小数点型 -length:10^length が最大値 -resolution:10^resolutionを1とする +length:1LL< + int length = 60, + int resolution = 27> class FixedPointImpl : private boost::operators> { private: - constexpr static long long shift = mypow(10ll, resolution); - constexpr static long long maxInt = - mypow(10ll, length - resolution); // FixedPointImplがとりうる整数の最大値 + constexpr static long long shift = 1LL << resolution; + constexpr static long long maxInt = 1LL << length; // FixedPointImplがとりうる整数の最大値 T value; - public: FixedPointImpl() : value(0) {} FixedPointImpl(const FixedPointImpl &v) : value(v.value) {} @@ -53,8 +37,8 @@ class FixedPointImpl : private boost::operators> template < typename U, std::enable_if_t< - std::is_arithmetic_v< - std::remove_reference_t> or std::is_convertible_v>, + std::is_arithmetic_v> + or std::is_convertible_v>, std::nullptr_t> = nullptr> FixedPointImpl(const U &v) { @@ -171,7 +155,7 @@ class FixedPointImpl : private boost::operators> } /* TODO: - オーバーフローの可能性が10^shift^2 = 10^12の掛け算でかなり高くなってしまうので + オーバーフローの可能性が shift^2 = 1LL<<54 の掛け算でかなり高くなってしまうので 一時的に浮動小数点に戻してから演算を行うことも考慮しておく */ FixedPointImpl &operator*=(const FixedPointImpl &obj) From 6dbc9d68cb924dbb05ea6f62f789f4a5e582f9fd Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 12 Jun 2023 01:25:15 +0000 Subject: [PATCH 2/2] fix:maxInt --- .../computation_container/fixed_point/fixed_point.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/server/computation_container/fixed_point/fixed_point.hpp b/packages/server/computation_container/fixed_point/fixed_point.hpp index daba1dd7f..45a963f59 100644 --- a/packages/server/computation_container/fixed_point/fixed_point.hpp +++ b/packages/server/computation_container/fixed_point/fixed_point.hpp @@ -14,10 +14,10 @@ namespace qmpc::Utils namespace mp = boost::multiprecision; using cpp_dec_float = mp::number>; /* -T:保持する整数型 -D:変換する浮動小数点型 -length:1LL<> { private: constexpr static long long shift = 1LL << resolution; - constexpr static long long maxInt = 1LL << length; // FixedPointImplがとりうる整数の最大値 + constexpr static long long maxInt = 1LL << (length-resolution); // FixedPointImplがとりうる整数の最大値 T value; public: FixedPointImpl() : value(0) {}