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 enable_if_t<!(sizeof(T) == 16 && numeric_traits<T>::digits == 64), bool>
34signbit(const T x) noexcept {
35 static_assert(is_floating_point_v<T>, "floating point required");
36 using UInt = make_integer_t<sizeof(T), false>;
37
38 const UInt bits = *reinterpret_cast<const UInt*>(&x);
39 constexpr UInt sign_mask = static_cast<UInt>(1) << (8 * sizeof(UInt) - 1);
40 return (bits & sign_mask) != 0;
41}
42
43template <typename T>
44NEFORCE_CONST_FUNCTION constexpr enable_if_t<sizeof(T) == 16 && numeric_traits<T>::digits == 64, bool>
45signbit(const T x) noexcept {
46 static_assert(is_floating_point_v<T>, "floating point required");
47
48 struct {
49 byte_t data[16];
50 } buf;
51 _NEFORCE memory_copy(&buf, &x, sizeof(buf));
52 return (buf.data[9] & 0x80) != 0;
53}
54
55
64template <typename T>
65NEFORCE_CONST_FUNCTION constexpr bool is_nan(const T x) noexcept {
66 static_assert(is_floating_point_v<T>, "floating point required");
67 return x != x;
68}
69
76template <typename T>
77NEFORCE_CONST_FUNCTION constexpr bool is_pos_infinity(const T x) noexcept {
78 static_assert(is_floating_point_v<T>, "floating point required");
79 return x == numeric_traits<T>::infinity();
80}
81
88template <typename T>
89NEFORCE_CONST_FUNCTION constexpr bool is_neg_infinity(const T x) noexcept {
90 static_assert(is_floating_point_v<T>, "floating point required");
91 return x == -numeric_traits<T>::infinity();
92}
93
101template <typename T>
102NEFORCE_CONST_FUNCTION constexpr bool is_infinity(const T x) noexcept {
103 return _NEFORCE is_pos_infinity(x) || _NEFORCE is_neg_infinity(x);
104}
105
114template <typename T>
115NEFORCE_CONST_FUNCTION constexpr bool is_finite(const T x) noexcept {
116 return !_NEFORCE is_infinity(x) && !_NEFORCE is_nan(x);
117}
118
128template <typename T>
129NEFORCE_CONST_FUNCTION constexpr bool is_normal(const T x) noexcept {
130 if (!_NEFORCE is_finite(x)) {
131 return false;
132 }
133 if (x == 0) {
134 return false;
135 }
136 const T abs = x < 0 ? -x : x;
137 return abs >= numeric_traits<T>::min();
138}
139
149template <typename T>
150NEFORCE_CONST_FUNCTION constexpr bool is_subnormal(const T x) noexcept {
151 if (!_NEFORCE is_finite(x)) {
152 return false;
153 }
154 if (x == 0) {
155 return false;
156 }
157 const T abs = x < 0 ? -x : x;
158 return abs < numeric_traits<T>::min();
159}
160
171template <typename T>
172NEFORCE_CONST_FUNCTION constexpr bool is_positive(const T x) noexcept {
173 return x > 0 || (x == 0 && !_NEFORCE signbit(x));
174}
175
186template <typename T>
187NEFORCE_CONST_FUNCTION constexpr bool is_negative(const T x) noexcept {
188 return x < 0 || (x == 0 && _NEFORCE signbit(x));
189}
190 // NumericTypeChecks
192
193NEFORCE_END_NAMESPACE__
194#endif // NEFORCE_CORE_NUMERIC_NUMERIC_TYPES_HPP__
数值类型极限特性主模板
static constexpr T infinity() noexcept
获取正无穷大表示
static constexpr T min() noexcept
获取类型的最小值
constexpr bool is_floating_point_v
is_floating_point的便捷变量模板
unsigned char byte_t
字节类型,定义为无符号字符
constexpr void * memory_copy(void *NEFORCE_RESTRICT dest, const void *NEFORCE_RESTRICT src, size_t count) noexcept
从源内存复制到目标内存
constexpr bool is_subnormal(const T x) noexcept
检查浮点数是否为次正规数
constexpr bool is_infinity(const T x) noexcept
检查浮点数是否为无穷大
constexpr enable_if_t<!(sizeof(T)==16 &&numeric_traits< T >::digits==64), bool > signbit(const T x) noexcept
获取浮点数的符号位
constexpr bool is_neg_infinity(const T x) noexcept
检查浮点数是否为负无穷大
constexpr bool is_nan(const T x) noexcept
检查浮点数是否为NaN
constexpr bool is_pos_infinity(const T x) noexcept
检查浮点数是否为正无穷大
constexpr bool is_negative(const T x) noexcept
检查浮点数是否为负数
constexpr bool is_finite(const T x) noexcept
检查浮点数是否为有限值
constexpr bool is_normal(const T x) noexcept
检查浮点数是否为正规数
constexpr bool is_positive(const T x) noexcept
检查浮点数是否为正数
typename make_integer< Size, IsSigned >::type make_integer_t
make_integer的便捷别名
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
内存操作函数
数值特征