if-clause似乎不在某些任意条件下执行

时间:2015-05-08 07:50:45

标签: perl debugging if-statement input

我试图将三角矩阵划分为几乎相同数量的元素。

我编写了以下代码,它适用于大多数输入组合,并将我的矩阵分割为从0到$length的给定数量的部分。

但是,有一些输入组合,如$length = 2003$number_of_segments = 50,其中输出中缺少最后一个段。 我测试了$threshold$total的值,但即使在那些奇怪的情况下它们似乎也是正确的。

你有什么想法,错误在哪里?

#!/usr/bin/perl

use strict;         #should always be used
use warnings;       #that one too
use autodie;        #just in case I forgot to check anything

my $length = shift or die "ERROR: Not enough arguments!\n";         #number of rows in the matrix
my $number_of_segments = shift or die "ERROR: Not enough arguments!\n";     #number of segments we want to get

my @segments = ÷         #array of segment-limits
print "$_\n" foreach @segments;

sub divide {
    my @segments = (0);         #the first segment starts at 0
    my $number_of_pairs = ($length*($length+1))/2;          #number of elements in matrix
    my $total = 0;          #counter for the elements we already visited
    my $segment_counter = 1;        #we are in the first segment

    for (my $i=0; $i<$length; $i++){        #going over the rows of the matrix
        $total += $length-$i;           #counting the elements in each row
        my $threshold = ($number_of_pairs/$number_of_segments)*$segment_counter;        #threshold for the next segment
        if ($total >= $threshold){          #if our current segment is large enough
            push @segments, $i+1;              #save the limit
            $segment_counter++;            #and open the next segment
        }
    }

    return @segments;
}

1 个答案:

答案 0 :(得分:1)

问题在于,由于精度有限,您通常无法比较浮点数的相等性。 $threshold的最终值略高(在我的32位Perl上为2007006.0000000002),因此您必须允许误差。

如果您将测试更改为

if ( $total + 1E-8 >= $threshold ) { ... }

然后你会得到你期望的结果。您可能必须调整增量值才能获得正确的结果。

请注意,这是一种非常缓慢且不准确的做事方式。您应该将所有算法保留为整数而不是浮点值,但我目前没有时间重构您的代码