[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: Incorrect handling of very small doubles?
From:       Abu Yoav <abuyoav () gmail ! com>
Date:       2010-09-22 1:56:20
Message-ID: AANLkTikghqhbzZqE0CGWjb1Giuf_V0efEcrRHKwDn_rJ () mail ! gmail ! com
[Download RAW message or body]


Hi Jon,

Thanks, again. However, I took a different route. I looked at the response
to Shlomi's question on this in perl5-porters. The responder suggested
adding

use POSIX qw(strtod);

and then replacing your calls to "parse_sci_note" with "strtod". My code
works now. Any drawback to this method?



On Tue, Sep 21, 2010 at 11:00 AM, Jon Hermansen <jon.hermansen@gmail.com>wrote:

> Hi Abu,
>
>  I got the idea for my variation on your script from a forum, where
> someone is parsing a list of numbers in scientific notation just as
> you are.
>
>
> http://www.gamedev.net/community/forums/topic.asp?topic_id=537620&whichpage=1&#3470722
>
> Until the bug is fixed, here's a workaround:
>
> ---
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $d1 = parse_sci_note('6.892964e-309');
> my $d2 = parse_sci_note('1.102874e-307');
>
> my $sum = $d1 + $d2;
>
> printf("d1 = %e\n", $d1);
> printf("d2 = %e\n", $d2);
> printf("sum = %e\n", $sum);
>
> sub parse_sci_note {
> # argumnet must be a string to work around perl bug
>    my $arg = shift;
>    $arg =~ s/[eE]/\*10\*\*/;
>    return eval($arg);
> }
>
> ---
>
> The perl documentation clearly shows scientific notation used here:
>
> http://perldoc.perl.org/perldata.html#Scalar-value-constructors
>
> so I'd say it definitely is a bug.
>
> On Tue, Sep 21, 2010 at 10:25 AM, Abu Yoav <abuyoav@gmail.com> wrote:
> > 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.hermansen@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 <abuyoav@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
> >> >
> >
> >
>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic