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

In reply to Re^7: Decimal Floating Point (DFP) and does Perl needs DFP? by syphilis
in thread Decimal Floating Point (DFP) and does Perl needs DFP? by flexvault

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.