28 #ifndef YB_INC_ystdex_rational_hpp_
29 #define YB_INC_ystdex_rational_hpp_ 1
46 template<
typename _tDst,
typename _tSrc>
50 static_assert(is_unsigned<_tSrc>::value,
"Non-integer type found.");
61 template<
typename _type>
72 typedef std::int64_t
type;
78 typedef std::uint64_t
type;
98 template<
typename _tBase = std::int32_t,
99 size_t _vInt = std::numeric_limits<_tBase>::digits - 6U,
100 size_t _vFrac = std::numeric_limits<_tBase>::digits - _vInt>
102 fixed_point<_tBase, _vInt, _vFrac>, operators::unit_steppable<
103 fixed_point<_tBase, _vInt, _vFrac>, operators::shiftable<fixed_point<
104 _tBase, _vInt, _vFrac>, size_t>>>
106 static_assert(std::is_integral<_tBase>::value,
"Non-integral type found.");
107 static_assert(_vInt <
size_t(std::numeric_limits<_tBase>::digits),
108 "No sufficient fractional bits found.");
109 static_assert(_vInt + _vFrac ==
size_t(std::numeric_limits<_tBase>::digits),
110 "Wrong total bits found.");
112 template<
typename _OtherBase,
size_t _vOtherInt,
size_t _vOtherFrac>
114 friend class std::numeric_limits<
fixed_point<_tBase, _vInt, _vFrac>>;
148 template<
typename _tInt>
151 std::is_integral<_tInt>::value,
int>::type = 0)
ynothrow
154 template<
typename _tFloat>
156 fixed_point(_tFloat val,
typename std::enable_if<std::is_floating_point<
164 template<
size_t _vOtherInt,
size_t _vOtherFrac>
167 typename std::enable_if<(_vOtherInt <
int_bit_n),
int>::type = 0)
171 template<
size_t _vOtherInt,
size_t _vOtherFrac>
174 typename std::enable_if<(
int_bit_n < _vOtherInt),
int>::type = 0)
240 + std::is_signed<base_type>::value);
242 value = tmp < 0 ? -(-tmp >> shift_bit_n) : tmp >> shift_bit_n;
273 template<
typename _type>
275 operator _type()
const
277 return this->cast<_type>();
281 template<
typename _type>
282 inline typename std::enable_if<std::is_integral<_type>::value,
288 template<
typename _type>
289 typename std::enable_if<std::is_floating_point<_type>::value,
322 return x.
value < 0 ? -x : x;
357 template<
typename _tBase, ystdex::
size_t _vInt, ystdex::
size_t _vFrac>
358 class numeric_limits<ystdex::fixed_point<_tBase, _vInt, _vFrac>>
370 return fp_type(std::numeric_limits<base_type>::min(),
377 return fp_type(std::numeric_limits<base_type>::max(),
390 static yconstexpr bool is_signed = numeric_limits<base_type>::is_signed;
415 static yconstexpr float_denorm_style has_denorm = denorm_absent;
444 static yconstexpr bool is_modulo = numeric_limits<base_type>::is_modulo;
446 static yconstexpr bool traps = numeric_limits<base_type>::traps;
447 static yconstexpr bool tinyness_before =
false;
448 static yconstexpr float_round_style round_style = round_toward_zero;