List::Util 是否改变了我的号码的表示形式?

发布于 2024-12-01 12:29:30 字数 1098 浏览 1 评论 0原文

这是需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆° 2024-12-08 12:29:30

这是一个舍入数值表示问题。

来自 perldoc perlfaq4 : 为什么我得到的是长小数(例如,19.9499999999999)而不是我应该得到的数字(例如,19.95)?


有关详细解释,请参阅 David Goldberg 的“每台计算机是什么”
科学家应该了解浮点运算”
(http://web.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf)。

在内部,您的计算机以二进制表示浮点数。
数字(如二的幂)计算机无法存储所有数字
确切地。一些实数在此过程中会失去精度。这是一个
计算机如何存储数字并影响所有计算机的问题
语言,而不仅仅是 Perl。

perlnumber 显示数字的详细信息
表示和转换。限制小数位数
在您的数字中,您可以使用 printfsprintf 函数。

有关更多详细信息,请参阅 perlop 中的浮点算术。< /p>

printf "%.2f", 10/3;
我的 $number = sprintf "%.2f", 10/3;

换句话说,如果这是一个问题,请使用 sprintf 解决它:

$variance = sprintf '%.9f', $todays_latency - $average ;  # Rounded to 9 d.p.

This is a rounding numerical 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)? :


For the long explanation, see David Goldberg's "What Every Computer
Scientist Should Know About Floating-Point Arithmetic"
(http://web.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf).

Internally, your computer represents floating-point numbers in binary.
Digital (as in powers of two) computers cannot store all numbers
exactly. Some real numbers lose precision in the process. This is a
problem with how computers store numbers and affects all computer
languages, not just Perl.

perlnumber shows the gory details of number
representations and conversions. To limit the number of decimal places
in your numbers, you can use the printf or sprintf function.

See Floating Point Arithmetic in perlop for more details.

printf "%.2f", 10/3;
my $number = sprintf "%.2f", 10/3;

In other words, if this is an issue, round it using sprintf:

$variance = sprintf '%.9f', $todays_latency - $average ;  # Rounded to 9 d.p.
埖埖迣鎅 2024-12-08 12:29:30
printf "Todays Variance from the average is %.9f\n", $variance;
printf "Todays Variance from the average is %.9f\n", $variance;
最单纯的乌龟 2024-12-08 12:29:30

顺便说一句,

my $sum = sum $ecp_average[1] + $ecp_average[2] +
              $ecp_average[3] + $ecp_average[4];

没有任何意义。您只将一个数字传递给 sum,因此它实际上是一项无操作。你想要

my $sum = $ecp_average[1] + $ecp_average[2] +
          $ecp_average[3] + $ecp_average[4];

my $sum = sum @ecp_average[1..4];

[这确实应该是一条评论,但作为评论它不会清晰易读。请原谅这个位置。]

By the way,

my $sum = sum $ecp_average[1] + $ecp_average[2] +
              $ecp_average[3] + $ecp_average[4];

makes no sense. You are only passing one number to sum, so it's effectively a no-op. You want

my $sum = $ecp_average[1] + $ecp_average[2] +
          $ecp_average[3] + $ecp_average[4];

or

my $sum = sum @ecp_average[1..4];

[This should really be a comment, but it wouldn't be legible as a comment. Please pardon the placement.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文