[sf-perl] Question on Perl math

Dajen Huang Dajen.Huang at Sun.COM
Fri Apr 13 15:05:52 PDT 2007


Hello,

Does anyone know why Perl put out .00000000001 on the 30th time of the
following calculation, then back to normal on the 82th time?

I use Perl for microprocessor design and that +/-0.000000001 really
kills me. I assume numbers are converted to full 64 bits floating point
then perform the calculation. But still doesn't make sense to me for
this to happen. They are small numbers so no danger of overflow. Plus I
am not doing '1 divided by 3' type of thing. It's a pure add. I know use
Math::BigFloat ; solves the problem. But that is too much trouble for a
simple add.

Thanks a lot.

Dajen

---------------------------------------
#!/bin/perl
# This is run on Solaris 10 Unix box
$a = 3569;
$b = 8.32;

$foo1 = 3569.28;

while ($i++ < 100) {
    $foo1 += $b;
    print "$foo1\n";
}
----------------------------------------

3577.6
3585.92
3594.24
3602.56
3610.88
3619.2
3627.52
3635.84
3644.16
3652.48
3660.8
3669.12
3677.44
3685.76
3694.08
3702.4
3710.72
3719.04
3727.36
3735.68
3744
3752.32
3760.64
3768.96
3777.28
3785.6
3793.92
3802.24
3810.56
3818.88000000001
3827.20000000001
3835.52000000001
3843.84000000001
3852.16000000001
3860.48000000001
3868.80000000001
3877.12000000001
3885.44000000001
3893.76000000001
3902.08000000001
3910.40000000001
3918.72000000001
3927.04000000001
3935.36000000001
3943.68000000001
3952.00000000001
3960.32000000001
3968.64000000001
3976.96000000001
3985.28000000001
3993.60000000001
4001.92000000001
4010.24000000001
4018.56000000001
4026.88000000001
4035.20000000001
4043.52000000001
4051.84000000001
4060.16000000001
4068.48000000001
4076.80000000001
4085.12000000001
4093.44000000001
4101.76000000001
4110.08000000001
4118.40000000001
4126.72000000001
4135.04000000001
4143.36000000001
4151.68000000001
4160.00000000001
4168.32000000001
4176.64000000001
4184.96000000001
4193.28000000001
4201.60000000001
4209.92000000001
4218.24000000001
4226.56000000001
4234.88000000001
4243.20000000001
4251.52
4259.84
4268.16
4276.48
4284.8
4293.12
4301.44
4309.76
4318.08
4326.4
4334.72
4343.04
4351.36
4359.68
4368
4376.32
4384.64
4392.96
4401.28




More information about the SanFrancisco-pm mailing list