Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions packages/server/computation_container/fixed_point/fixed_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,34 @@

namespace qmpc::Utils
{
template <typename T, typename U>
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<mp::cpp_dec_float<50>>;
/*
T:保持する整数型
D:変換する浮動小数点型
length:10^length が最大値
resolution:10^resolutionを1とする
T : 保持する整数型
D : 変換する浮動小数点型
length : 使用するTのbit長
resolution : 1LL<<resolutionを1とする
*/
template <
typename T = boost::multiprecision::cpp_int,
typename D = cpp_dec_float,
int length = 18,
int resolution = 8>
int length = 60,
int resolution = 27>
class FixedPointImpl : private boost::operators<FixedPointImpl<>>
{
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-resolution); // FixedPointImplがとりうる整数の最大値
T value;

public:
FixedPointImpl() : value(0) {}
FixedPointImpl(const FixedPointImpl &v) : value(v.value) {}
FixedPointImpl(FixedPointImpl &&v) : value(std::move(v.value)) {}
template <
typename U,
std::enable_if_t<
std::is_arithmetic_v<
std::remove_reference_t<U>> or std::is_convertible_v<T, std::remove_reference_t<U>>,
std::is_arithmetic_v<std::remove_reference_t<U>>
or std::is_convertible_v<T, std::remove_reference_t<U>>,
std::nullptr_t> = nullptr>
FixedPointImpl(const U &v)
{
Expand Down Expand Up @@ -171,7 +155,7 @@ class FixedPointImpl : private boost::operators<FixedPointImpl<>>
}
/*
TODO:
オーバーフローの可能性が10^shift^2 = 10^12の掛け算でかなり高くなってしまうので
オーバーフローの可能性が shift^2 = 1LL<<54 の掛け算でかなり高くなってしまうので
一時的に浮動小数点に戻してから演算を行うことも考慮しておく
*/
FixedPointImpl &operator*=(const FixedPointImpl &obj)
Expand Down