#ifndef INCLUDED_IEEE_FLOAT_H #define INCLUDED_IEEE_FLOAT_H #ifndef INCLUDED_STDINT_H #define INCLUDED_STDINT_H #include #endif /*INCLUDED_STDINT_H*/ #ifndef INCLUDED_FLOAT_H #define INCLUDED_FLOAT_H #include #endif /*INCLUDED_FLOAT_H*/ #ifndef INCLUDED_STDBOOL_H #define INCLUDED_STDBOOL_H #include #endif /*INCLUDED_STDBOOL_H*/ /* ------------------------------------------------------------------------------------------------------------------ */ /* */ typedef struct { union{ struct{ uint32_t sign:1; uint32_t exponent:8; uint32_t mantissa:23; }IEEE754; uint32_t uint; float f; }value; }ieee_float_t; typedef struct { union{ struct{ uint64_t sign:1; uint64_t exponent:11; uint64_t mantissa:52; }IEEE754; uint64_t uint; double f; }value; }ieee_double_t; /* ------------------------------------------------------------------------------------------------------------------ */ /* */ #define IEEE_FLOAT_POSITIVE_INFINITY (1.0f/0.0f) #define IEEE_FLOAT_NEGATIVE_INFINITY (-1.0f/0.0f) #define IEEE_FLOAT_NAN (0.0f/0.0f) #define IEEE_FLOAT_EPSILON (FLT_EPSILON) #define IEEE_DOUBLE_EPSILON (DBL_EPSILON) /* ------------------------------------------------------------------------------------------------------------------ */ /* */ int ieee_float_cmp(const float a, const float b, const float maxDiff); int ieee_double_cmp(const double a, const double b, const double maxDiff); // 当将 float 看作 uint32 时进行比较,maxUlpsDiff 就是作为无符号整数允许的最大差异,一般取 `1` 就可以 // maxFloatDiff 一般取 IEEE_FLOAT_EPSILON, 是将数值作为 float 进行比较,但是如果两个 float 无法完成比较,就可以通过 // 无符号整数的形式来进行比较 int ieee_float_UlpsCmp(const float a, const float b, const float maxFloatDiff, const int maxUlpsDiff); int ieee_double_UlpsCmp(const double a, const double b, const double maxFloatDiff, const int maxUlpsDiff); bool ieee_float_is_zero(const float a, const float maxDiff); bool ieee_double_is_zero(const double a, const double maxDiff); #endif /*INCLUDED_IEEE_FLOAT_H*/