本文主要是介绍[C++]直接访问float变量内存的类,addEpison和subEpison、almostEqual约等于,及浮点数排序函数。从此忘记0.000001 再也不要使用FLT_EPSILON!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
探究浮点数奥秘,这里给出一个直接访问float变量内存的类,二进制兼容float,
并给出addEpison和subEpison两个函数
而这两个函数,是模糊比较所需要的方法的基础。
//负数时随着内存值int变大,浮点值绝对值越大
// 80000000 →→→ bf800000 → ff7fffff ff800000 ff800001 ffffffff
// -0 渐变 -1 渐变 -MAX -INF -Nan -Nan
//
// 正数时随着内存值int变大,浮点值绝对值越大
// 00000000 →→→ 3f800000 → ff7fffff 7f7fffff 7f800001 7fffffff
// +0 渐变 +1 渐变 +MAX +INF +Nan +Nan//从负到正遍历浮点数的方法: ff800000 -> 80000000, 00000000 -> 7f7fffff
// -INF -0 +0 +INFstruct Float
{union{struct{unsigned int Mantissa : 23;unsigned int Exponent : 8;unsigned int Sign : 1;};struct {unsigned int withoutSign : 31;unsigned int : 1;};int _memoryInt = 0;float _float;};operator float&() { return _float; }operator const float&() const { return _float; }operator float() const { return _float; }Float() = default;Float(float f) : _memoryInt((int&)f){}bool equalZero() { return withoutSign == 0; }bool isNan() { return Exponent == 0xFF && Mantissa != 0; }//是正无穷大bool isINF_P() { return _memoryInt == 0x7F800000; }//是负无穷大bool isINF_N() { return _memoryInt == 0xFF800000; }//是无穷大bool isINF_PN() { return (_memoryInt & 0x7F800000) == 0x7F800000; }//非数值void makeNan() { _memoryInt = 0x7FFFFFFF; } //正Nan中内存值最大的//正无穷大void makeINF_P() { _memoryInt = 0x7F800000; }//负无穷大void makeINF_N() { _memoryInt = 0xFF800000; }void makeMax() { _memoryInt = 0x7F7FFFFF; }void makeMax_N() { _memoryInt = 0xFF7FFFFF; }void makeMin() { _memoryInt = 0x00800000; } //最小值1.1754943510e-38(0x00800000), 次小值1.175494491e-38(0x00800001)void makeMin_N() { _memoryInt = 0x80800000; }void makeTrueMin() { _memoryInt = 0x00000001; } //最小值1.4012984643e-45F(0x00000001),次小值2.803e-45 2.8025969286496341e-45 (0x00800002)void makeTrueMin_N() { _memoryInt = 0x80000001; }//增加一个极小值,使得比当前值大;+max,+/-INF,+/-Nan不受影响void addEpison(){if ((unsigned int)_memoryInt >= 0xFF800000u) return; //负无穷大,负Nanif (_memoryInt >= 0x7F7FFFFF) return; //正最大值,正无穷大,正Nanif (_memoryInt == 0x80000000) //负0_memoryInt = 0x00000001; //TrueMinelse{_memoryInt += 1 - ((_memoryInt<0)<<1);}}//减少一个极小值,是的比当前值小void subEpison() {if ((unsigned int)_memoryInt >= 0xFF800000u) return; //负最大值,负无穷大,负Nanif (_memoryInt >= 0x7F800000) return; //正无穷大,正Nanif (_memoryInt == 0x00000000) //正0_memoryInt = 0x80000001; //TrueNegMinelse{_memoryInt -= 1 - ((_memoryInt < 0) << 1);}}bool almostEqual(const Float& other, unsigned int episonCount) const{if (((unsigned int)_memoryInt >= 0xFF800000u) || (_memoryInt >= 0x7F800000)|| ((unsigned int)other._memoryInt >= 0xFF800000u) || (other._memoryInt >= 0x7F800000))return _float == other._float; //无穷大,Nanint absA = _memoryInt < 0 ? 0x80000000 - _memoryInt : _memoryInt;int absB = other._memoryInt < 0 ? 0x80000000 - other._memoryInt : other._memoryInt;return (episonCount + unsigned int(absA - absB)) <= (episonCount << 1);}
};
Float a = 1.f / 3;Float b = 0.5f - 1.f / 6;a += a;b += b;{bool test = a == b;puts(test ? "true" : "false"); //false}{bool test = a.almostEqual(b, 1); //一个精度差距?puts(test ? "true" : "false"); //true}{Float A = a;A.addEpison(); //手动增加一个精度差距bool test = A.almostEqual(b, 1); //一个精度差距?puts(test ? "true" : "false"); //false}{Float A = a;A.addEpison(); //手动增加一个精度差距bool test = A.almostEqual(b, 2); //二个精度差距?puts(test ? "true" : "false"); //true}{Float x1 = a, x2 = a;x1.addEpison();x2.subEpison();bool test2 = a == b || x1 == b || x2 == b;puts(test2 ? "true" : "false"); //true}
下面给出一个浮点数排序函数,使用他能正确使包含无穷大、Nan的浮点数组成功排序
[](float a, float b) {bool isnanA = isnan(a), isnanB = isnan(b);if (isnanA || isnanB){if (!isnanB){return ((int&)a & 0x80000000) != 0;}if (!isnanA){return ((int&)b & 0x80000000) == 0; }if (((int&)a < 0) != ((int&)b < 0))return (int&)a < (int&)b;else //两nan符号相同return (((int&)a & 0x7FFFFF) < ((int&)b & 0x7FFFFF)) != ((int&)a < 0);}if ((int&)a == (int&)b)return false;else if (a < b)return true;else if (b < a)return false;elsereturn (int&)a < (int&)b;}
这篇关于[C++]直接访问float变量内存的类,addEpison和subEpison、almostEqual约等于,及浮点数排序函数。从此忘记0.000001 再也不要使用FLT_EPSILON!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!