日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

四舍五入VS銀行家舍入

系統(tǒng) 1851 0

相信細(xì)心的程序員們?cè)缇桶l(fā)現(xiàn)了 .net 環(huán)境下默認(rèn)舍入算法的是“四舍六入”的算法。從小學(xué)我們就學(xué)過“四舍五入”算法,但是更加科學(xué)的舍入辦法應(yīng)該是“四舍六入”,也就是今天我們要討論的“ 銀行家舍入 ”。

大家可以做一個(gè) Demo 。

C# 環(huán)境下

        
             1:  
        
        
          class
        
         Program
      
        
             2:  
        
            {
      
        
             3:  
        
        
          static
        
        
          void
        
         Main(
        
          string
        
        [] args)
      
        
             4:  
        
                { 
      
        
             5:  
        
        
          do
        
      
        
             6:  
        
                   {
      
        
             7:  
        
                        Console.WriteLine(
        
          "請(qǐng)輸入一個(gè)小數(shù)回車測(cè)試,輸入其他回車結(jié)束測(cè)試"
        
        );
      
        
             8:  
        
        
          string
        
         Num = Console.ReadLine();
      
        
             9:  
        
        
          try
        
      
        
            10:  
        
                        {
      
        
            11:  
        
                            Console.WriteLine(
        
          "結(jié)果為"
        
         + Convert.ToInt16(Convert.ToDouble(Num)));
      
        
            12:  
        
                        }
      
        
            13:  
        
        
          catch
        
         (Exception e) {
      
        
            14:  
        
        
          break
        
        ;
      
        
            15:  
        
                        } 
      
        
            16:  
        
                   }
      
        
            17:  
        
        
          while
        
         (
        
          true
        
         );
      
        
            18:  
        
                }
      
        
            19:  
        
            }
      

得到的結(jié)果如下

<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

image

VB.net 環(huán)境下測(cè)試代碼為

        
             1:  
        
            Sub Main()
      
        
             2:  
        
                Do
      
        
             3:  
        
                    Console.WriteLine(
        
          "請(qǐng)輸入一個(gè)小數(shù)回車測(cè)試,輸入其他回車結(jié)束測(cè)試。"
        
        )
      
        
             4:  
        
                    Try
      
        
             5:  
        
                        Dim a As String = Console.ReadLine()
      
        
             6:  
        
                        Console.WriteLine(
        
          "結(jié)果為:"
        
         & CInt(Convert.ToDouble(a)))
      
        
             7:  
        
                    Catch ex As Exception
      
        
             8:  
        
                        Exit Sub
      
        
             9:  
        
                    End Try
      
        
            10:  
        
                Loop
      
        
            11:  
        
            End Sub
      

結(jié)果如下

image

完全符合銀行家舍入的規(guī)律:四舍六入五考慮,五后非零就進(jìn)一,五后為零看奇偶,五前為偶應(yīng)舍去,五前為奇要進(jìn)一

關(guān)于 VB.net 中的 CInt 微軟的 MSDN 上有具體說明

Fractional Parts. When you convert a nonintegral value to an integral type, the integer conversion functions ( CByte , CInt , CLng , CSByte , CShort , CUInt , CULng , and CUShort ) remove the fractional part and round the value to the closest integer.

If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding , and its purpose is to compensate for a bias that could accumulate when adding many such numbers together.

相對(duì)于四舍五入,銀行家舍入的確更加的準(zhǔn)確,討論如下:

有些童鞋可能認(rèn)為在一般性的測(cè)量中,最后一位小數(shù)位上 0 9 出現(xiàn)的概率是相等的。一共十個(gè)數(shù)字, 0 4 可以舍去(四舍), 5 9 可以進(jìn)位(五入),多么完美的舍入算法!

但是!您可能忽略了一點(diǎn),末尾的 0 在這里是相當(dāng)于 10 還是相當(dāng)于 0

為了避免混沌,請(qǐng)看下圖:

<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

image

圖中是用 Matlab 畫的一個(gè)簡(jiǎn)單的數(shù)軸,可以看出 0.0 0.1 、 0.2 0.3 、 0.4 、 0.5 0.6 0.7 0.8 0.9 、 1.0 0.0 1.0 都是 0 結(jié)尾所以不能確定測(cè)量數(shù)據(jù)中的 0 是哪個(gè)零!

還是看上圖,圖中只要不滿 0.5 都按照 0 算,大于 0.5 都按照 1.0 算,那么剩下的 0.5 怎么辦?為了體現(xiàn)平均性,看上一位是奇數(shù)還是偶數(shù),如果是奇數(shù)則進(jìn)位,如果是偶數(shù)則舍去。這正是“銀行家舍入”的思想。這樣一來便達(dá)到了相對(duì)于“四舍五入”舍入方法更加平衡的舍入算法。

PS :“銀行家舍入”是 IEEE 規(guī)定的舍入標(biāo)準(zhǔn)。因此所有符合 IEEE 標(biāo)準(zhǔn) 的語(yǔ)言都是采用這一算法的。

四舍五入VS銀行家舍入


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 广汉市| 邻水| 浦城县| 慈利县| 乌拉特后旗| 扬中市| 芒康县| 清涧县| 伽师县| 故城县| 兰州市| 蓬安县| 崇仁县| 盱眙县| 轮台县| 平湖市| 桃源县| 若尔盖县| 辽中县| 新丰县| 玛沁县| 海城市| 山阴县| 通许县| 泽库县| 仁布县| 额尔古纳市| 日土县| 信宜市| 噶尔县| 盐边县| 息烽县| 县级市| 五峰| 教育| 六枝特区| 宜良县| 西丰县| 大港区| 什邡市| 平泉县|