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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

略有不同的浮點數(shù)學(xué)結(jié)果(C 到 golang)

略有不同的浮點數(shù)學(xué)結(jié)果(C 到 golang)

Go
慕標(biāo)琳琳 2021-11-08 10:08:46
我正在直接在 golang 中開發(fā)一個技術(shù)指標(biāo)庫。除其他外,它是學(xué)習(xí) golang 的練習(xí)。我一直在通過使用 TA-Lib(或者更確切地說是圍繞 TA-Lib 的 ruby 包裝器)生成的數(shù)據(jù)構(gòu)建測試用例來驗證我的算法的結(jié)果。在我開始實施布林帶之前,這一直運行良好。我的實現(xiàn)似乎工作正常,但在小數(shù)點后 14-15 位有所不同。我已經(jīng)閱讀了不同編程語言中的 Floating point math并懷疑這可能是罪魁禍?zhǔn)祝ㄎ乙陨晕⒉煌捻樞蜻M(jìn)行計算)。編輯添加:上面的問題涉及浮點數(shù)學(xué)的一個非常簡單的表現(xiàn)形式。確認(rèn)一段較長的代碼實際上解決了這個問題要困難得多。由于順序,我如何確認(rèn)它只是浮點數(shù)學(xué)的變化?/ 結(jié)束編輯我的理解正確嗎?這是我的實現(xiàn):package taimport (  "math")func BollingerBands(values []float64, period int) ([]float64, []float64, []float64) {  deviationsUp := 2.0  deviationsDown := 2.0  middleBand := Sma(values, period)  offset := len(values)-len(middleBand)  var upperBand []float64  var lowerBand []float64  for idx, v := range middleBand {    backIdx := offset+idx-period+1    curIdx := offset+idx+1    if backIdx < 0 {      backIdx = 0    }    stdDev := SliceStdDev(values[backIdx:curIdx])    upperBand = append(upperBand, v + (stdDev * deviationsUp))    lowerBand = append(lowerBand, v - (stdDev * deviationsDown))  }  return upperBand, middleBand, lowerBand}// Sma produces the Simple Moving Average for the// supplied array of float64 values for a given periodfunc Sma(values []float64, period int) []float64{  var result []float64  for index,_ := range values {    indexPlusOne := index+1    if(indexPlusOne>=period) {      avg := Mean(values[indexPlusOne-period:indexPlusOne])      result = append(result, avg)    }  }  return result}// SliceMean returns the Mean of the slice of float64func SliceMean(values []float64) float64 {  var total float64=0    for _,element := range values {        total += element    }  return total / float64(len(values))}// SliceVariance returns the variance of the slice of float64.func SliceVariance(values []float64) float64 {    if 0 == len(values) {        return 0.0    }    m := SliceMean(values)    var sum float64    for _, v := range values {        d := v - m        sum += d * d    }    return sum / float64(len(values))}
查看完整描述

2 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗 獲得超7個贊

我認(rèn)為算法是不同的。例如方差:


/* Do the MA calculation using tight loops. */

/* Add-up the initial periods, except for the last value. */

periodTotal1 = 0;

periodTotal2 = 0;

trailingIdx = startIdx-nbInitialElementNeeded;


i=trailingIdx;

if( optInTimePeriod > 1 )

{

   while( i < startIdx ) {

      tempReal = inReal[i++];

      periodTotal1 += tempReal;

      tempReal *= tempReal;

      periodTotal2 += tempReal;

   }

}


/* Proceed with the calculation for the requested range.

 * Note that this algorithm allows the inReal and

 * outReal to be the same buffer.

 */

outIdx = 0;

do

{

   tempReal = inReal[i++];


   /* Square and add all the deviation over

    * the same periods.

    */


   periodTotal1 += tempReal;

   tempReal *= tempReal;

   periodTotal2 += tempReal;


   /* Square and add all the deviation over

    * the same period.

    */


   meanValue1 = periodTotal1 / optInTimePeriod;

   meanValue2 = periodTotal2 / optInTimePeriod;


   tempReal = inReal[trailingIdx++];

   periodTotal1 -= tempReal;

   tempReal *= tempReal;

   periodTotal2 -= tempReal;


   outReal[outIdx++] = meanValue2-meanValue1*meanValue1;

} while( i <= endIdx );

這看起來不像你的方差。如果您要重現(xiàn)算法以便它們執(zhí)行完全相同的操作,那么 Go 版本應(yīng)該產(chǎn)生相同的結(jié)果。Go 只是在做標(biāo)準(zhǔn)的 IEEE 754 浮點運算。


至于“順序重要嗎?”這個問題。確實如此。由于浮點算術(shù)不精確,您在計算時會丟失信息。大多數(shù)時候它不會產(chǎn)生太大的不同,但有時算法可能非常容易受到這些變化的影響。(因此以代數(shù)方式重新排列您的公式可能不會在實際代碼中得到相同的答案)


您經(jīng)常會在這些庫中發(fā)現(xiàn)算法旨在解決這些問題,因此它們通??雌饋聿幌袷怯字傻膶崿F(xiàn)。例如mean,通常是一個微不足道的函數(shù),但以下是它在 GSL 中的計算方式:


double

FUNCTION (gsl_stats, mean) (const BASE data[], const size_t stride, const size_t size)

{

  /* Compute the arithmetic mean of a dataset using the recurrence relation 

     mean_(n) = mean(n-1) + (data[n] - mean(n-1))/(n+1)   */


  long double mean = 0;

  size_t i;


  for (i = 0; i < size; i++)

    {

      mean += (data[i * stride] - mean) / (i + 1);

    }


  return mean;

}

因此,除非您完全匹配算法,否則您的答案將略有不同。(這并不一定意味著你的程序是錯誤的)


通常用于此的一種解決方案是在一個非常小的數(shù)字(math.Abs(expected-result) < ?,您定義 ?: 的地方const ? = 0.0000001)內(nèi)進(jìn)行相等比較,而不是使用==.


查看完整回答
反對 回復(fù) 2021-11-08
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊

正如 Caleb 和 Matteo 的評論/答案所建議的那樣,即使代碼排序方式的細(xì)微差異也會導(dǎo)致浮點值的差異。

我最終確認(rèn),至少在一個小樣本量的情況下,完全像 TA-Lib 一樣實現(xiàn)代碼會產(chǎn)生正確的浮點值。正如預(yù)期的那樣,即使稍微偏離 TA-Lib (C) 實現(xiàn)也會導(dǎo)致浮點值的微小差異。



查看完整回答
反對 回復(fù) 2021-11-08
  • 2 回答
  • 0 關(guān)注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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