On Sat, 08 Oct 2005 20:33:16 +0900
dune <FZH01112@...> さんwrote:
> perl gomi.pl 3 が 1 になるのが不思議。
こんな具合になって動いています。
結果としては、use bignumでは合計$bは1になっているように見えるのだけど、
なぜか、$bを1と比較するとNGになる場合がある。use bignumを使わない場合に
は、比較の結果通り、$bが1になっていない場合だけ、NGになっている。小数点
以下を30桁まで表示させても同じでした。
@nums = (1,2,3,4,5,6,7,8,9,10);
foreach $n (@nums){
$b += 1/$n foreach(1..$n);
$eq = ($b == 1 ? "OK" : "NG");
printf "\$n = %f -> %s 1/\$n = %.20f \$b = %.20f \n", $n, $eq, 1/$n, $b;
$b = 0;
}
C:\Scripts\Perl>perl TMm2.pl
$n = 1.000000 -> OK 1/$n = 1.00000000000000000000 $b = 1.00000000000000000000
$n = 2.000000 -> OK 1/$n = 0.50000000000000000000 $b = 1.00000000000000000000
$n = 3.000000 -> OK 1/$n = 0.33333333333333331000 $b = 1.00000000000000000000
$n = 4.000000 -> OK 1/$n = 0.25000000000000000000 $b = 1.00000000000000000000
$n = 5.000000 -> OK 1/$n = 0.20000000000000001000 $b = 1.00000000000000000000
$n = 6.000000 -> NG 1/$n = 0.16666666666666666000 $b = 0.99999999999999989000
$n = 7.000000 -> NG 1/$n = 0.14285714285714285000 $b = 0.99999999999999978000
$n = 8.000000 -> OK 1/$n = 0.12500000000000000000 $b = 1.00000000000000000000
$n = 9.000000 -> NG 1/$n = 0.11111111111111110000 $b = 1.00000000000000020000
$n = 10.000000 -> NG 1/$n = 0.10000000000000001000 $b = 0.99999999999999989000
C:\Scripts\Perl>perl -Mbignum TMm2.pl
$n = 1.000000 -> OK 1/$n = 1.00000000000000000000 $b = 1.00000000000000000000
$n = 2.000000 -> OK 1/$n = 0.50000000000000000000 $b = 1.00000000000000000000
$n = 3.000000 -> NG 1/$n = 0.33333333333333331000 $b = 1.00000000000000000000
$n = 4.000000 -> OK 1/$n = 0.25000000000000000000 $b = 1.00000000000000000000
$n = 5.000000 -> OK 1/$n = 0.20000000000000001000 $b = 1.00000000000000000000
$n = 6.000000 -> NG 1/$n = 0.16666666666666669000 $b = 1.00000000000000000000
$n = 7.000000 -> NG 1/$n = 0.14285714285714285000 $b = 1.00000000000000000000
$n = 8.000000 -> OK 1/$n = 0.12500000000000000000 $b = 1.00000000000000000000
$n = 9.000000 -> NG 1/$n = 0.11111111111111112000 $b = 1.00000000000000000000
$n = 10.000000 -> OK 1/$n = 0.10000000000000001000 $b = 1.00000000000000000000
藤岡 和夫
kazuf@...
TS Networkのために http://homepage1.nifty.com/kazuf/