12 #if !defined(SG14_FIXED_POINT_EXTRAS_H) 13 #define SG14_FIXED_POINT_EXTRAS_H 1 15 #include "fixed_point_type.h" 27 template<
class Rep,
int Exponent>
28 struct get_rep<fixed_point<Rep, Exponent>> {
32 template<
class OldRep,
int Exponent,
class NewRep>
33 struct set_rep<fixed_point<OldRep, Exponent>, NewRep> {
34 using type = fixed_point<NewRep, Exponent>;
41 template<
class Rep,
int Exponent>
42 struct digits<fixed_point<Rep, Exponent>> : digits<Rep> {
45 template<
class Rep,
int Exponent, _digits_type MinNumBits>
46 struct set_digits<fixed_point<Rep, Exponent>, MinNumBits> {
47 using type = fixed_point<set_digits_t<Rep, MinNumBits>, Exponent>;
50 template<
class Rep,
int Exponent,
class Value>
51 struct from_value<fixed_point<Rep, Exponent>, Value> {
52 using type = fixed_point<Value>;
58 template<
class Rep,
int Exponent>
59 constexpr
auto abs(
const fixed_point<Rep, Exponent>& x) noexcept
62 return (x >= 0) ?
static_cast<decltype(-x)
>(x) : -x;
72 constexpr Rep sqrt_bit(Rep n, Rep bit)
74 return (bit>n) ? sqrt_bit<Rep>(n, bit >> 2) : bit;
78 constexpr Rep sqrt_bit(Rep n)
80 return sqrt_bit<Rep>(n, Rep(1) << ((digits<Rep>::value + is_signed<Rep>::value) - 2));
84 constexpr Rep sqrt_solve3(
92 static_cast<Rep
>(n-(result+bit)),
94 static_cast<Rep
>((result >> 1)+bit))
95 : sqrt_solve3<Rep>(n, bit >> 2, result >> 1)
100 constexpr Rep sqrt_solve1(Rep n)
102 return sqrt_solve3<Rep>(n, sqrt_bit<Rep>(n), Rep{0});
127 template<
class Rep,
int Exponent>
128 constexpr fixed_point <Rep, Exponent>
133 #if defined(SG14_EXCEPTIONS_ENABLED) 134 (x<fixed_point<Rep, Exponent>(0))
135 ?
throw std::invalid_argument(
"cannot represent square root of negative value") :
138 static_cast<Rep>(_impl::fp::extras::sqrt_solve1(widened_type{x}.data())));
151 template<
class Rep,
int Exponent, _impl::fp::
float_of_same_size<Rep>(* F)(
152 _impl::fp::
float_of_same_size<Rep>)>
156 using floating_point = _impl::fp::float_of_same_size<Rep>;
163 template<
class Rep,
int Exponent>
167 return _impl::fp::extras::crib<Rep, Exponent, std::sin>(x);
170 template<
class Rep,
int Exponent>
174 return _impl::fp::extras::crib<Rep, Exponent, std::cos>(x);
177 template<
class Rep,
int Exponent>
181 return _impl::fp::extras::crib<Rep, Exponent, std::exp>(x);
184 template<
class Rep,
int Exponent>
188 return _impl::fp::extras::crib<Rep, Exponent, std::pow>(x);
194 template<
class Rep,
int Exponent>
195 ::std::ostream& operator<<(::std::ostream& out, const fixed_point <Rep, Exponent>& fp)
197 return out << static_cast<long double>(fp);
200 template<
class Rep,
int Exponent>
217 template<
class Rep,
int Exponent>
218 struct numeric_limits<sg14::
fixed_point<Rep, Exponent>>
219 : std::numeric_limits<sg14::_impl::number_base<sg14::fixed_point<Rep, Exponent>, Rep>> {
222 using _rep =
typename _value_type::rep;
223 using _rep_numeric_limits = numeric_limits<_rep>;
227 static constexpr _value_type min() noexcept
229 return _value_type::from_data(_rep{1});
232 static constexpr _value_type max() noexcept
234 return _value_type::from_data(_rep_numeric_limits::max());
237 static constexpr _value_type lowest() noexcept
239 return _value_type::from_data(_rep_numeric_limits::lowest());
242 static constexpr
bool is_integer =
false;
244 static constexpr _value_type epsilon() noexcept
246 return _value_type::from_data(_rep{1});
249 static constexpr _value_type round_error() noexcept
251 return _value_type::from_data(_rep{0});
254 static constexpr _value_type infinity() noexcept
256 return _value_type::from_data(_rep{0});
259 static constexpr _value_type quiet_NaN() noexcept
261 return _value_type::from_data(_rep{0});
264 static constexpr _value_type signaling_NaN() noexcept
266 return _value_type::from_data(_rep{0});
269 static constexpr _value_type denorm_min() noexcept
271 return _value_type::from_data(_rep{1});
276 #endif // SG14_FIXED_POINT_EXTRAS_H constexpr fixed_point< Rep, Exponent > sqrt(const fixed_point< Rep, Exponent > &x)
calculates the square root of a fixed_point value
Definition: fixed_point_extras.h:129
static constexpr fixed_point from_data(rep const &r)
creates an instance given the underlying representation value
Definition: fixed_point_type.h:190
literal real number approximation that uses fixed-point arithmetic
Definition: fixed_point_type.h:20
study group 14 of the C++ working group
Definition: const_integer.h:22