3 回答
TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊
編寫(xiě)一個(gè)有用的通用浮點(diǎn)IsEqual是非常非常困難的,如果不是完全不可能的話。您當(dāng)前的代碼將嚴(yán)重失敗a==0。該方法應(yīng)該如何處理這種情況實(shí)際上是一個(gè)定義的問(wèn)題,并且可以說(shuō)代碼最適合特定的域用例。
對(duì)于這種事情,你真的需要一個(gè)好的測(cè)試套件。這就是我為浮點(diǎn)指南做的,這就是我最終提出的(Java代碼,應(yīng)該很容易翻譯):
public static boolean nearlyEqual(float a, float b, float epsilon) {
final float absA = Math.abs(a);
final float absB = Math.abs(b);
final float diff = Math.abs(a - b);
if (a == b) { // shortcut, handles infinities
return true;
} else if (a == 0 || b == 0 || absA + absB < Float.MIN_NORMAL) {
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < (epsilon * Float.MIN_NORMAL);
} else { // use relative error
return diff / (absA + absB) < epsilon;
}}您還可以在網(wǎng)站上找到測(cè)試套件。
附錄: c#中的相同代碼用于雙打(在問(wèn)題中提到)
public static bool NearlyEqual(double a, double b, double epsilon){
const double MinNormal = 2.2250738585072014E-308d;
double absA = Math.Abs(a);
double absB = Math.Abs(b);
double diff = Math.Abs(a - b);
if (a.Equals(b))
{ // shortcut, handles infinities
return true;
}
else if (a == 0 || b == 0 || absA + absB < MinNormal)
{
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < (epsilon * MinNormal);
}
else
{ // use relative error
return diff / (absA + absB) < epsilon;
}}TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
從Bruce Dawson關(guān)于比較浮點(diǎn)數(shù)的論文中,您還可以將浮點(diǎn)數(shù)作為整數(shù)進(jìn)行比較。接近度由最低有效位確定。
public static bool AlmostEqual2sComplement( float a, float b, int maxDeltaBits ) {
int aInt = BitConverter.ToInt32( BitConverter.GetBytes( a ), 0 );
if ( aInt < 0 )
aInt = Int32.MinValue - aInt; // Int32.MinValue = 0x80000000
int bInt = BitConverter.ToInt32( BitConverter.GetBytes( b ), 0 );
if ( bInt < 0 )
bInt = Int32.MinValue - bInt;
int intDiff = Math.Abs( aInt - bInt );
return intDiff <= ( 1 << maxDeltaBits );}編輯:BitConverter相對(duì)較慢。如果您愿意使用不安全的代碼,那么這是一個(gè)非??斓陌姹荆?/p>
public static unsafe int FloatToInt32Bits( float f )
{
return *( (int*)&f );
}
public static bool AlmostEqual2sComplement( float a, float b, int maxDeltaBits )
{
int aInt = FloatToInt32Bits( a );
if ( aInt < 0 )
aInt = Int32.MinValue - aInt;
int bInt = FloatToInt32Bits( b );
if ( bInt < 0 )
bInt = Int32.MinValue - bInt;
int intDiff = Math.Abs( aInt - bInt );
return intDiff <= ( 1 << maxDeltaBits );
}TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
繼Andrew Wang的回答:如果BitConverter方法太慢但你不能在你的項(xiàng)目中使用不安全的代碼,這個(gè)結(jié)構(gòu)比BitConverter快6倍:
[StructLayout(LayoutKind.Explicit)]public struct FloatToIntSafeBitConverter{
public static int Convert(float value)
{
return new FloatToIntSafeBitConverter(value).IntValue;
}
public FloatToIntSafeBitConverter(float floatValue): this()
{
FloatValue = floatValue;
}
[FieldOffset(0)]
public readonly int IntValue;
[FieldOffset(0)]
public readonly float FloatValue;}(順便說(shuō)一句,我嘗試使用已接受的解決方案,但它(至少我的轉(zhuǎn)換)失敗了一些在答案中也提到的單元測(cè)試。例如assertTrue(nearlyEqual(Float.MIN_VALUE, -Float.MIN_VALUE));)
- 3 回答
- 0 關(guān)注
- 1196 瀏覽
添加回答
舉報(bào)
