gpt4 book ai didi

ruby-on-rails - 使计算不准确的 float

转载 作者:数据小太阳 更新时间:2023-10-29 08:35:51 24 4
gpt4 key购买 nike

我在ruby中没有设置变量的datetype,我用的是默认的datatype,导致最后计算不准确,怎么解决?

   scores = params[:scores].split("\r\n").map { |n| n.to_f }  # array of scores (at most 2 decimal points,e.g 2.32 ,23,65.76.....)

#new array
sd=Array.new(scores.length,0)


sd[0]= (scores[0]-average)**2 # calculation <<== sd[i] become 234.08999999999992
470.8900000000001
13.69000000000002
0.6399999999999955
86.48999999999995

sd[1]= (scores[1]-average)**2
sd[2]= (scores[2]-average)**2
sd[3]= (scores[3]-average)**2
sd[4]= (scores[4]-average)**2

sd_sum=sd[0]+sd[1]+sd[2]+sd[3]+sd[4]

sd_sum=sd_sum**2 ...
gg=sd_sum/5 # further calculation
ans=Math.sqrt(gg) # final answer for standard deviation

由于最终答案经过多步计算,结果不准确。

最佳答案

为了保持计算的准确性,请使用 BigDecimal而不是 Float :

 scores = params[:scores].split("\r\n").map { |n| BigDecimal(n) }

BigDecimal provides support for very large or very accurate floating point numbers.

Decimal arithmetic is also useful for general calculation, because it provides the correct answers people expect–whereas normal binary floating point arithmetic often introduces subtle errors because of the conversion between base 10 and base 2.

关于ruby-on-rails - 使计算不准确的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com