如何修复这个 Perl 代码,使 1.1 + 2.2 == 3.3? [英] How do I fix this Perl code so that 1.1 + 2.2 == 3.3?

查看:10
本文介绍了如何修复这个 Perl 代码,使 1.1 + 2.2 == 3.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修复此代码以使 1.1 + 2.2 == 3.3?这里实际发生了什么导致这种行为?我对舍入问题和浮点数学有点熟悉,但我认为这仅适用于除法和乘法,并且会在输出中可见.

How do I fix this code so that 1.1 + 2.2 == 3.3? What is actually happening here that's causing this behavior? I'm vaguely familiar with rounding problems and floating point math, but I thought that applied to division and multiplication only and would be visible in the output.

[me@unixbox1:~/perltests]> cat testmathsimple.pl 
#!/usr/bin/perl

use strict;
use warnings;

check_math(1, 2, 3);
check_math(1.1, 2.2, 3.3);

sub check_math {
        my $one = shift;
        my $two = shift;
        my $three = shift;

        if ($one + $two == $three) {
                print "$one + $two == $three
";
        } else {
                print "$one + $two != $three
";
        }
}

[me@unixbox1:~/perltests]> perl testmathsimple.pl 
1 + 2 == 3
1.1 + 2.2 != 3.3

到目前为止,大多数答案都类似于这是一个浮点问题,呵呵",并为它提供了解决方法.我已经怀疑这是问题所在.我该如何展示它?如何让 Perl 输出长格式的变量?将 $one + $two 计算存储在临时变量中并打印它并不能说明问题.

Most of the answers thus far are along the lines of "it's a floating point problem, duh" and are providing workarounds for it. I already suspect that to be the problem. How do I demonstrate it? How do I get Perl to output the long form of the variables? Storing the $one + $two computation in a temp variable and printing it doesn't demonstrate the problem.

使用 aschepler 演示的 sprintf 技术,我现在能够看到"问题.此外,按照 mscha 和 rafl 的建议,使用 bignum 可以解决比较不相等的问题.但是, sprintf 输出仍然表明数字不正确".这让人对这个解决方案有点怀疑.

Using the sprintf technique demonstrated by aschepler, I'm now able to "see" the problem. Further, using bignum, as recommended by mscha and rafl, fixes the problem of the comparison not being equal. However, the sprintf output still indicates that the numbers aren't "correct". That's leaving a modicum of doubt about this solution.

bignum 是解决这个问题的好方法吗?在将其集成到更大的现有程序中时,我们应该注意 bignum 的任何可能的副作用吗?

Is bignum a good way to resolve this? Are there any possible side effects of bignum that we should look out for when integrating this into a larger, existing, program?

推荐答案

请参阅 每个计算机科学家都应该知道的内容关于浮点运算.

这些都不是 Perl 特有的:有无数个实数,显然,它们都不能只用有限的位数来表示.

None of this is Perl specific: There are an uncountably infinite number of real numbers and, obviously, all of them cannot be represented using only a finite number of bits.

要使用的具体解决方案"取决于您的具体问题.您是否正在尝试跟踪货币金额?如果是这样,请使用 bignum 提供的任意精度数(使用更多内存和更多 CPU,获得更准确的结果).你在做数值分析吗?然后,决定你要使用的精度,并使用sprintf(如下图)和eq进行比较.

The specific "solution" to use depends on your specific problem. Are you trying to track monetary amounts? If so, use the arbitrary precision numbers (use more memory and more CPU, get more accurate results) provided by bignum. Are you doing numeric analysis? Then, decide on the precision you want to use, and use sprintf (as shown below) and eq to compare.

您可以随时使用:

use strict; use warnings;

check_summation(1, $_) for [1, 2, 3], [1.1, 2.2, 3.3];

sub check_summation {
    my $precision = shift;
    my ($x, $y, $expected) = @{ $_[0] };
    my $result = $x + $y;

    for my $n ( $x, $y, $expected, $result) {
        $n = sprintf('%.*f', $precision, $n);
    }

    if ( $expected eq $result ) {
        printf "%s + %s = %s
", $x, $y, $expected;
    }
    else {
        printf "%s + %s != %s
", $x, $y, $expected;
    }
    return;
}

输出:

1.0 + 2.0 = 3.0
1.1 + 2.2 = 3.3

这篇关于如何修复这个 Perl 代码,使 1.1 + 2.2 == 3.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆