1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include "[MathFunc.h]"19 #include "[myassert.h]"20 21 [MATH_BEGIN_NAMESPACE]22 23 24 bool [EqualAbs](float a, float b, float [epsilon])25 {26 return [Abs](a-b) < [epsilon];27 }28 29 float [RelativeError](float a, float b)30 {31 if (a == b) return 0.f; 32 return [Abs]((a-b)/[Max]([Abs](a), [Abs](b)));33 }34 35 bool [EqualRel](float a, float b, float maxRelError)36 {37 if (a == b) return true; 38 return [Abs](a-b) <= maxRelError * [Max]([Abs](a), [Abs](b));39 }40 41 inline int [ReinterpretFloatAsInt](float a)42 {43 union reinterpret_float_as_int44 {45 float f;46 int i;47 };48 reinterpret_float_as_int fi;49 fi.f = a;50 return fi.i;51 }52 53 bool [EqualUlps](float a, float b, int maxUlps)54 {55 [assert](sizeof(float) == sizeof(int));56 [assert](maxUlps >= 0);57 [assert](maxUlps < 4 * 1024 * 1024);58 59 int intA = [ReinterpretFloatAsInt](a);60 if (intA < 0) intA = 0x80000000 - intA;61 int intB = [ReinterpretFloatAsInt](b);62 if (intB < 0) intB = 0x80000000 - intB;63 if ([Abs](intA - intB) <= maxUlps)64 return true;65 return false;66 }67 68 [MATH_END_NAMESPACE] Go back to previous page