我想要做的类似的东西其他功能
避免位字段@David,因为它取决于具体实现。 <math.h>
包含用于对float
进行分类的宏。这些宏也适用于double
和long double
。
#include <math.h>
// Adjust returns values as desired.
int is_infinity_is_zero_is_denormal(float f) {
if (isinf(f)) return 'i';
if (f == 0.0f) return 'z';
if (isnan(f)) return 'n';
if (isnormal(f)) return 0; // neither zero, subnormal, infinite, nor NaN
// All that is left is de-normal/sub-normal.
return 'd';
}
或者,也许只是
bool is_infinity_is_zero_is_denormal(float f) {
return !(isnormal(f) || isnan(f));
}
另见int fpclassify(real-floating x);
的数量在一个步骤分类。
将其参数值分类为NaN,无限,正常,低于正常值的零或其他实现定义的类别。 C11§7.12.3.12
bool is_infinity_is_zero_is_denormal(float f) {
return fpclassify(f) & (FP_INFINITE | FP_ZERO | FP_SUBNORMAL);
}
让编译器处理的优化。
['std :: isinf'](http://en.cppreference.com/w/cpp/numeric/math/isinf)等可能['std :: fpclassify'](http:// en。 cppreference.com/w/cpp/numeric/math/FP_categories),如果你是各种各样的值。 –
@FrançoisAndrieux - 没关系。 'fpclassify'是C标准库的一部分。 – StoryTeller
@StoryTeller'std :: fpclassify'虽然不是 –