NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
numeric_types.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_NUMERIC_NUMERIC_TYPES_HPP__
2#define NEFORCE_CORE_NUMERIC_NUMERIC_TYPES_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
21
32template <typename T>
33NEFORCE_CONST_FUNCTION constexpr bool signbit(const T x) noexcept {
34 static_assert(is_floating_point_v<T>, "floating point required");
35 using UInt = make_integer_t<sizeof(T), false>;
36
37 const UInt bits = *reinterpret_cast<const UInt*>(&x);
38 constexpr UInt sign_mask = static_cast<UInt>(1) << (8 * sizeof(UInt) - 1);
39 return (bits & sign_mask) != 0;
40}
41
42
51template <typename T>
52NEFORCE_CONST_FUNCTION constexpr bool is_nan(const T x) noexcept {
53 static_assert(is_floating_point_v<T>, "floating point required");
54 return x != x;
55}
56
63template <typename T>
64NEFORCE_CONST_FUNCTION constexpr bool is_pos_infinity(const T x) noexcept {
65 static_assert(is_floating_point_v<T>, "floating point required");
66 return x == numeric_traits<T>::infinity();
67}
68
75template <typename T>
76NEFORCE_CONST_FUNCTION constexpr bool is_neg_infinity(const T x) noexcept {
77 static_assert(is_floating_point_v<T>, "floating point required");
78 return x == -numeric_traits<T>::infinity();
79}
80
88template <typename T>
89NEFORCE_CONST_FUNCTION constexpr bool is_infinity(const T x) noexcept {
90 return _NEFORCE is_pos_infinity(x) || _NEFORCE is_neg_infinity(x);
91}
92
101template <typename T>
102NEFORCE_CONST_FUNCTION constexpr bool is_finite(const T x) noexcept {
103 return !_NEFORCE is_infinity(x) && !_NEFORCE is_nan(x);
104}
105
115template <typename T>
116NEFORCE_CONST_FUNCTION constexpr bool is_normal(const T x) noexcept {
117 if (!_NEFORCE is_finite(x)) {
118 return false;
119 }
120 if (x == 0) {
121 return false;
122 }
123 const T abs = x < 0 ? -x : x;
124 return abs >= numeric_traits<T>::min();
125}
126
136template <typename T>
137NEFORCE_CONST_FUNCTION constexpr bool is_subnormal(const T x) noexcept {
138 if (!_NEFORCE is_finite(x)) {
139 return false;
140 }
141 if (x == 0) {
142 return false;
143 }
144 const T abs = x < 0 ? -x : x;
145 return abs < numeric_traits<T>::min();
146}
147
158template <typename T>
159NEFORCE_CONST_FUNCTION constexpr bool is_positive(const T x) noexcept {
160 return x > 0 || (x == 0 && !_NEFORCE signbit(x));
161}
162
173template <typename T>
174NEFORCE_CONST_FUNCTION constexpr bool is_negative(const T x) noexcept {
175 return x < 0 || (x == 0 && _NEFORCE signbit(x));
176}
177 // NumericTypeChecks
179
180NEFORCE_END_NAMESPACE__
181#endif // NEFORCE_CORE_NUMERIC_NUMERIC_TYPES_HPP__
static NEFORCE_NODISCARD constexpr T min() noexcept
获取类型的最小值
static NEFORCE_NODISCARD constexpr T infinity() noexcept
获取正无穷大表示
NEFORCE_INLINE17 constexpr bool is_floating_point_v
is_floating_point的便捷变量模板
NEFORCE_CONST_FUNCTION constexpr bool is_nan(const T x) noexcept
检查浮点数是否为NaN
NEFORCE_CONST_FUNCTION constexpr bool is_infinity(const T x) noexcept
检查浮点数是否为无穷大
NEFORCE_CONST_FUNCTION constexpr bool is_finite(const T x) noexcept
检查浮点数是否为有限值
NEFORCE_CONST_FUNCTION constexpr bool is_pos_infinity(const T x) noexcept
检查浮点数是否为正无穷大
NEFORCE_CONST_FUNCTION constexpr bool is_neg_infinity(const T x) noexcept
检查浮点数是否为负无穷大
NEFORCE_CONST_FUNCTION constexpr bool is_subnormal(const T x) noexcept
检查浮点数是否为次正规数
NEFORCE_CONST_FUNCTION constexpr bool signbit(const T x) noexcept
获取浮点数的符号位
NEFORCE_CONST_FUNCTION constexpr bool is_positive(const T x) noexcept
检查浮点数是否为正数
NEFORCE_CONST_FUNCTION constexpr bool is_normal(const T x) noexcept
检查浮点数是否为正规数
NEFORCE_CONST_FUNCTION constexpr bool is_negative(const T x) noexcept
检查浮点数是否为负数
typename make_integer< Size, IsSigned >::type make_integer_t
make_integer的便捷别名
数值特征
类型萃取