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?
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. :)
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 |