第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

C#的浮點(diǎn)比較函數(shù)

C#的浮點(diǎn)比較函數(shù)

揚(yáng)帆大魚(yú) 2019-08-12 18:41:13
C#的浮點(diǎn)比較函數(shù)有人可以在C#中指向(或顯示)一些好的通用浮點(diǎn)比較函數(shù)來(lái)比較浮點(diǎn)值嗎?我想實(shí)現(xiàn)的功能IsEqual,IsGreater一個(gè)IsLess。我也只關(guān)心雙打不漂浮。
查看完整描述

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;
    }}


查看完整回答
反對(duì) 回復(fù) 2019-08-12
?
largeQ

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 );
    }


查看完整回答
反對(duì) 回復(fù) 2019-08-12
?
幕布斯7119047

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));


查看完整回答
反對(duì) 回復(fù) 2019-08-12
  • 3 回答
  • 0 關(guān)注
  • 1196 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)