Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: Decimal Floating Point (DFP) and does Perl needs DFP?

by syphilis (Archbishop)
on Jan 19, 2015 at 03:48 UTC ( [id://1113697]=note: print w/replies, xml ) Need Help??


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

Calculating 10 * 1.000001 ** 6e7 should minimize the propagation of rounding errors, if exponentiation is sufficiently well implemented.

I have found a case where that's not so for the calculation in question - namely, Windows 7, perl-5.20.0, nvtype is double.
But the same perl version and configuration on Ubuntu supports your assertion. So it might just be that the Windows exponentiation (or something else there) is buggy.
I haven't experimented much at all, and don't really intend to.

What's the reasoning behind your assertion ?

Cheers,
Rob
  • Comment on Re^5: Decimal Floating Point (DFP) and does Perl needs DFP?

Replies are listed 'Best First'.
Re^6: Decimal Floating Point (DFP) and does Perl needs DFP?
by LanX (Saint) on Jan 19, 2015 at 09:41 UTC
    > What's the reasoning behind your assertion ? 

    Exponentiation laws.

    E.g. x^10 = x^8 * x^2 and x^8 = ((x^2)^2)^2

    Just because 10 is 1010 binary, hence < 4*2 multiplications.

    6e7 should have a 26 bit representation so <52 multiplications needed.

    Of course there are even faster implementations which are less exact. I think that's what you are observing.

    edit
    Of course a binary calculation should be done with integer x=1000001 without fraction, dividing later thru 10^(6*60) should be easy enough.

    Cheers Rolf

    PS: Je suis Charlie!

    update

    proof of concept -> here

      proof of concept -> here

      I don't think there's any guarantee that you'll get a more accurate result using that exponentiation approach.
      I ran the below script (on perl-5.16 with NV of "double") and found that sometimes your approach gave the best approximation, other times the "left-to-right" variant of your approach gave the best approximation, and other times just doing the series of multiplications gave the best approximation. (See the comments in the script.)
      I used the mpfr library as my reference for the correct 15 digit value - calculated with 1000 bits of precision (which is way overkill).
      It's true that your approach uses fewer calculations, but some of those squarings stand to really amplify the errors produced by the roundings.
      #!perl -l use strict; use warnings; use Math::MPFR qw(:mpfr); my $e=107; my $v = "5.34"; Rmpfr_set_default_prec(1000); mpfr($v, $e); print lanx($v, $e); # Wins for 5.34 ** 107 & 5.34 ** 100. print menezes($v, $e); # Wins for 5.231 ** 107 & 5.231 ** 100. print by_mul($v, $e); # Wins for 5.35 ** 107 & 5.35 ** 100. sub mpfr { my $val = shift; my $exp = shift; my $ret = Math::MPFR->new($val); $ret **= $e; Rmpfr_out_str($ret, 10, 15, MPFR_RNDN); printf "\n"; } sub lanx { my $val = shift; $val += 0; my $exp = shift; my $ret = 1; for my $bit (reverse split //,sprintf '%b',$exp) { $ret *= $val if $bit; $val **= 2; } return $ret; } sub menezes { my $val = shift; $val += 0; my $exp = shift; my $ret = 1; for my $bit (split //,sprintf '%b',$exp) { $ret **= 2; $ret *= $val if $bit; } return $ret; } sub by_mul { my $val = shift; $val += 0; my $exp = shift; my $ret = 1; $ret *= $val for 1..$exp; return $ret; }
      I could be wrong, of course. (It's happened before ;-)

      Cheers,
      Rob
        I don't really know much about MPFR.

        I needed a predictable accuracy for pow(6e7) of a decimal fraction. (a highly constructed use case)

        BigFloat is decimal thus avoiding binary rounding errors in this case and the limited number of multiplications allowed a boundary for the necessary precision.( 1 extra digit per multiplication)

        I have no doubt that other algorithms can be faster or more accurate with less digits.

        This whole thread is somehow spooky for me, financial transactions follow their own rules and rarely involve exponents bigger 100.

        Natural processes in science have no reason to be decimal. If a measurement is expressed as a decimal this already involves an errormargin. (bacterias don't grow exactly x%)

        I.e. decimals are normally human made!

        So if accuracy is a problem this can't be fully solved with a general purpose library.

        Eg IIRC do some fiscal authorities or banks calculate with special rounding units.

        That's not solved with 128 digits accuracy...

        Cheers Rolf

        PS: Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1113697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found