我试图将三个麻木3,4,6从num + 0.1增加到num + 0.7所有数字都正常工作,除了3.为什么?
foreach my $num(3,4,6){
for(my $state=$num+0.1;$state<=$num+0.7;$state+=0.1){
print $state."\n";
}
}
输出: 即使我硬编码并打印到3.6,这种情况仍在继续。
for(my $state=3;$state<=3.7;$state+=0.1){
print $state."\n";
}
答案 0 :(得分:1)
使用Math::BigFloat;
#!C:/path/of/perl.exe -w
use Math::BigFloat ':constant';
use warnings;
use strict;
foreach my $num(3,4,6) {
for(my $state=$num+0.1;$state<=$num+0.7;$state+=0.1){
print $state."\n";
}
}
输出:
3.1
3.2
3.3
3.4
3.5
3.6
3.7
4.1
4.2
4.3
4.4
4.5
4.6
4.7
6.1
6.2
6.3
6.4
6.5
6.6
6.7
答案 1 :(得分:1)
这是另一种选择:
use strict;
use warnings;
for my $num ( 3, 4, 6 ) {
for my $i ( 1 .. 7 ) {
print $num + $i / 10, "\n";
}
}
输出:
3.1
3.2
3.3
3.4
3.5
3.6
3.7
4.1
4.2
4.3
4.4
4.5
4.6
4.7
6.1
6.2
6.3
6.4
6.5
6.6
6.7