in reply to Re^2: Decimal Floating Point (DFP) and does Perl needs DFP?
in thread Decimal Floating Point (DFP) and does Perl needs DFP?

> would you mind stating the mathematically correct result (say, to six digits after decimal point)?

This should be correct for at least 50 digits after decimal point:

1141973130130727445029596475.97165373568561208619839490929389898529956 +04918087368059784750017004880305063884032742217053556637130

calculated using Math::BigFloat (which operates decimal with configurable precision) and taking advantage of exponentiation laws described here.

(NB: BigFloat's own ->bpow() method is far slower)

use strict; use warnings; use Math::BigFloat; my $exp=6e7; Math::BigFloat->precision(-100); my $x=Math::BigFloat->new('1.000001'); my $val=Math::BigFloat->new('1'); $\="\n"; for my $bit (reverse split //,sprintf '%b',$exp) { # print "$bit ",$x; $val->bmul($x) if $bit; $x->bpow(2); } $val->bmul(10); print "Result : $val"; my $syphilis=Math::BigFloat->new('1141973130130727445029596475.971760' +); print "diff: ", $val - $syphilis;

took about a second on a netbook.

Compilation started at Tue Jan 20 02:19:30 /usr/bin/perl -w /home/lanx/pm/big_expo.pl Result : 1141973130130727445029596475.97165373568561208619839490929389 +89852995604918087368059784750017004880305063884032742217053556637130 diff: -0.0001062643143879138016050907061010147004395081912631940215249 +982995119694936115967257782946443362870 Compilation finished at Tue Jan 20 02:19:31

The results from the C-libs are as you can see only accurate till the third digit after decimal point.

I'm not a big expert on BigFloat, if you see a problem, all feedback welcome. :)

Cheers Rolf

PS: Je suis Charlie!

update

you can easily increase the accuracy to 1000 digits after decimal point by setting precision to -1050, only takes a second.

Replies are listed 'Best First'.
Re^4: Decimal Floating Point (DFP) and does Perl needs DFP?
by syphilis (Archbishop) on Jan 20, 2015 at 03:24 UTC
    This should be correct for at least 50 digits after decimal point:
    1141973130130727445029596475.9716537356856120861983949092938989852995604918087368059784750017004880305063884032742217053556637130


    Yes, it's correct for 69 digits after the decimal point. The following, provided by Math::MPFR, appears to be correct for 129 digits - as increasing the mpfr precision above 450 bits doesn't alter the 129 digit value:
    11419731301307274450295964759.716537356856120861983949092938989852995604918087368059784750017004844856171227209226348596180640102

    And that agrees (to at least 129 digits) with LanX's script when M::BF precision is set to -1050.

    Cheers,
    Rob