Click here to get back home

mismatch between Perl 5.6 and Perl 5.8 in printing high precision values.

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
mismatch between Perl 5.6 and Perl 5.8 in printing high precision values. vivekanand.naik 04-02-2008
Get Chitika Premium
Posted by vivekanand.naik on April 2, 2008, 6:21 am
Please log in for more thread options
printf("%.32g\n",0.99999999976716936);

Perl 5.6.1 output:
0.99999999976716936 --> GOOD

Perl 5.8.6 output:
0.99999999976716925 --> ERROR


Any reason for such mismatch ?
Please let me know how to avoid that or any alternative approach if
any.

Thanks
Vivek

Posted by RedGrittyBrick on April 2, 2008, 7:02 am
Please log in for more thread options
vivekanand.naik@gmail.com wrote:
> printf("%.32g\n",0.99999999976716936);
>
> Perl 5.6.1 output:
> 0.99999999976716936 --> GOOD
>
> Perl 5.8.6 output:
> 0.99999999976716925 --> ERROR
>
>
> Any reason for such mismatch ?
> Please let me know how to avoid that or any alternative approach if
> any.
>

print Math::BigFloat->new("0.99999999976716936")->bstr();


--
RGB

Posted by szr on April 2, 2008, 11:53 am
Please log in for more thread options
RedGrittyBrick wrote:
> vivekanand.naik@gmail.com wrote:
>> printf("%.32g\n",0.99999999976716936);
>>
>> Perl 5.6.1 output:
>> 0.99999999976716936 --> GOOD
>>
>> Perl 5.8.6 output:
>> 0.99999999976716925 --> ERROR
>>
>>
>> Any reason for such mismatch ?
>> Please let me know how to avoid that or any alternative approach if
>> any.
>>
>
> print Math::BigFloat->new("0.99999999976716936")->bstr();


The version of Math::BigFloat that shipped with 5.6.1, which the op
seems to need, doesn't seem to support ->bstr()

$ perlall -MMath::BigFloat -e 'print
Math::BigFloat->new("0.99999999976716936")->bstr(), "\n";'
<perl5.10.0> 0.99999999976716936
<perl5.8.8 > 0.99999999976716936
<perl5.8.2 > 0.99999999976716936
<perl5.8.0 > 0.99999999976716936
Can't locate object method "bstr" via package "Math::BigFloat" (perhaps
you forgot to load "Math::BigFloat"?) at -e line 1.
<perl5.6.1 >


Perhaps ->ffround(17) could be used instead?

$ perl5.6.1 -MMath::BigFloat -e 'print
Math::BigFloat->new("0.99999999976716936")->ffround(-17), "\n";'
+99999999976716936E-17

You could always update your Math::* modules if you must use 5.6.1,
else, it would better to just install a newer Perl :-)

--
szr



Posted by Mirco Wahab on April 2, 2008, 7:18 am
Please log in for more thread options
vivekanand.naik@gmail.com wrote:
> printf("%.32g\n",0.99999999976716936);
>
> Perl 5.6.1 output:
> 0.99999999976716936 --> GOOD
>
> Perl 5.8.6 output:
> 0.99999999976716925 --> ERROR
> Any reason for such mismatch ?
> Please let me know how to avoid that or any alternative approach if
> any.

This is in the interval from 1e-16 to 1e-15, which
is the accuracy of a IEEE 8 byte double in this
number range (52 bits for fraction). Seems to
be ok ==> 1 / (2^52).
The correct result (above) needs (imho) longer
than 8 byte doubles, like

perl -MMath::BigFloat -e'print
Math::BigFloat->new("0.99999999976716936")->bstr()'


Regards

M.


Posted by sisyphus on April 2, 2008, 9:46 am
Please log in for more thread options
On Apr 2, 9:21=A0pm, vivekanand.n...@gmail.com wrote:
> printf("%.32g\n",0.99999999976716936);
>
> Perl 5.6.1 output:
> 0.99999999976716936 =A0--> GOOD
>
> Perl 5.8.6 output:
> 0.99999999976716925 =A0--> ERROR
>
> Any reason for such mismatch ?

I believe it's a bug in perl 5.8 (which has been carried through to
perl 5.10):

C:\_32\pscrpt>perl -e "printf(\"%.32g\n\",0.99999999976716936);"
0.99999999976716925

C:\_32\pscrpt>perl -e "printf(\"%.32g\n\",9.9999999976716936e-1);"
0.99999999976716936

Since 0.99999999976716936 =3D=3D 9.9999999976716936e-1, I can think of no
good reason that that those 2 one-liners should produce different
output - and I believe the first one liner produces an incorrect
result.

I'll submit a bug report about this - unless someone can convince me
that it's *not* a bug (or unless someone else wants to submit the
report).

Cheers,
Rob

Similar ThreadsPosted
Q: string convert to number and any high precision soluation? December 9, 2006, 10:57 am
How to send email from perl-script with different priorities : normal or high January 10, 2008, 2:02 pm
windows program return values vs perl return values from a call to system() -- windows post only October 29, 2005, 6:22 pm
Perl printing to system May 22, 2006, 1:56 pm
printing with perl v5.8.8 on xp pro system May 28, 2006, 11:28 am
printing envelops from perl July 10, 2008, 3:38 pm
Printing arrow keys through perl January 6, 2005, 1:48 am
Prototype mismatch under ModPerl::PerlRun September 6, 2005, 5:59 pm
Re: Data type mismatch warning using DBI September 5, 2008, 3:14 pm
fcntl "F_SETLKW" value mismatch / linux header bug? October 23, 2005, 6:39 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap