Hi Jon and Shlomi,

Thanks!

Jon:
Actually, in my case, it's a bit more complicated than that. I read the
numbers from a file, and there they are in the standard C notation that I've
used in my example.

Shlomi:
Thanks for the heads up about how to write good code. I'm still very much a
beginner...
Also, thanks for consulting perl5-porters. If it is a bug, and it is fixed,
then I will indeed port my C code to perl.



On Mon, Sep 20, 2010 at 11:40 PM, Jon Hermansen <jon.herman...@gmail.com>wrote:

> Hi Abu,
>  This code works for me:
>
> #!/usr/bin/perl
>
> my $d1, $d2, $sum;
>
> $d1 = 6.892964 * 10 ** -309;
> $d2 = 1.102874 * 10 ** -307;
>
> $sum = $d1 + $d2;
>
> printf("d1 = %e\n", $d1);
> printf("d2 = %e\n", $d2);
> printf("sum = %e\n", $sum);
>
> On Mon, Sep 20, 2010 at 1:30 PM, Abu Yoav <abuy...@gmail.com> wrote:
> > Hi,
> >
> > I wanted to port a small c program to perl. However, the two programs
> behave
> > differently with respect to very small "double" values. The behavior
> seems
> > like a bug to me, but since I'm very new to perl, I thought I might ask
> the
> > list first.
> >
> > Relevant code and output follows. Thanks!
> >
> > --- c code ---
> > #include<stdio.h>
> >
> > int main (int argc, char** argv)
> > {
> > double d1, d2, sum;
> >
> > d1 = 6.892964e-309;
> > d2 = 1.102874e-307;
> >
> > sum = d1 + d2;
> >
> > printf("d1 = %e\n", d1);
> > printf("d2 = %e\n", d2);
> > printf("sum = %e\n", sum);
> >
> > return 0;
> > }
> >
> > --- perl code ---
> > #!/usr/bin/perl
> >
> > my $d1, $d2, $sum;
> >
> > $d1 = "6.892964e-309";
> > $d2 = "1.102874e-307";
> >
> > $sum = $d1 + $d2;
> >
> > printf("d1 = %e\n", $d1);
> > printf("d2 = %e\n", $d2);
> > printf("sum = %e\n", $sum);
> >
> > --- c output ---
> > d1 = 6.892964e-309
> > d2 = 1.102874e-307
> > sum = 1.171804e-307
> >
> > --- perl output ---
> > d1 = 0.000000e+00
> > d2 = 1.000000e-307
> > sum = 1.000000e-307
> >
>

Reply via email to