List::Util 是否改变了我的号码的表示形式?
这是需要 4 天信息的脚本的结尾部分, 计算前三天的平均值,然后从平均值中减去今天的值 以获得方差。
第一个例子是正确的。 然而,如果从 0.002997575 减去 0.00299268,第二个示例等于 -0.000004895。但是 List::Util
将其列为 - 4.89499999999955e-06。
我需要以常规表示法得到它。
use List::Util qw/sum/;
$todays_latency = $ecp_average[0];
$sum = sum $ecp_average[1] + $ecp_average[2] + $ecp_average[3] + $ecp_average[4];
$average = $sum/$#ecp_average;
$variance = $todays_latency - $average ;
print "Todays listing is $todays_latency\n";
print "The Average is $average\n";
print "Todays Variance from the average is $variance\n";
print "\n";
foreach(@ecp_average){
print "$_\n";
}
print "\n";
@ecp_average = ();
}
输出
Eislnd1
Todays listing is 0.00376258
The Average is 0.004412365
Todays Variance from the average is -0.000649785
0.00376258
0.00371207
0.00511266
Eislnd2
Todays listing is 0.00299268
The Average is 0.002997575
Todays Variance from the average is -4.89499999999955e-06
0.00299268
0.00301986
0.00297529
This is the end part of a script that takes 4 days information,
averages the previous three days, then subtracts todays value from the average
to get the variance.
The first example is correct.
However the second example if you subtract 0.00299268 from 0.002997575 equals -0.000004895. However List::Util
is listing it as -4.89499999999955e-06.
I need to get it in regular notation.
use List::Util qw/sum/;
$todays_latency = $ecp_average[0];
$sum = sum $ecp_average[1] + $ecp_average[2] + $ecp_average[3] + $ecp_average[4];
$average = $sum/$#ecp_average;
$variance = $todays_latency - $average ;
print "Todays listing is $todays_latency\n";
print "The Average is $average\n";
print "Todays Variance from the average is $variance\n";
print "\n";
foreach(@ecp_average){
print "$_\n";
}
print "\n";
@ecp_average = ();
}
Output
Eislnd1
Todays listing is 0.00376258
The Average is 0.004412365
Todays Variance from the average is -0.000649785
0.00376258
0.00371207
0.00511266
Eislnd2
Todays listing is 0.00299268
The Average is 0.002997575
Todays Variance from the average is -4.89499999999955e-06
0.00299268
0.00301986
0.00297529
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个
舍入数值表示问题。来自
perldoc perlfaq4 :
为什么我得到的是长小数(例如,19.9499999999999)而不是我应该得到的数字(例如,19.95)?:换句话说,如果这是一个问题,请使用 sprintf 解决它:
This is a
roundingnumerical representation issue.From
perldoc perlfaq4 :
Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? :In other words, if this is an issue, round it using
sprintf
:顺便说一句,
没有任何意义。您只将一个数字传递给
sum
,因此它实际上是一项无操作。你想要或
[这确实应该是一条评论,但作为评论它不会清晰易读。请原谅这个位置。]
By the way,
makes no sense. You are only passing one number to
sum
, so it's effectively a no-op. You wantor
[This should really be a comment, but it wouldn't be legible as a comment. Please pardon the placement.]