Another solution based on the fact that a floating point number is actually a sum of powers of 2.

For 0 <= y <= 0, it holds that y = b_{0} *2^{0} + b_{1} *2^{-1} + b_{2} *2^{-2} + b_{3} *2^{-3} + ...

And so, x^{y} = x^{b0*20 + b1*2-1 + b2*2-2 + b3*2-3 + ...} = x^{b0*20} * x^{b1*2-1} * x^{b2*2-2} * x^{b3*2-3} * ... = Π_{i, bi≠0} x^{2-i}

Which in Perl becomes...

#!/usr/bin/perl
use strict;
use warnings;
my ($X, $Y, $n) = @ARGV;
$n ||= 20;
my $Z = $X ** $Y;
my ($neg, $y) = ($Y < 0 ? (1, -$Y) : (0, $Y));
$y > 1 and die "Y is out of range";
my $bit = 1;
my $x = $X;
my $z = 1;
while ($y) {
if ($y >= $bit) {
$y -= $bit;
$z *= $x;
}
$bit /= 2;
$x = sqrt($x);
}
$z = 1/$z if $neg;
my $e = abs($z - $X ** $Y);
print "z=$z, e=$e\n";

sqrt is also an expensive operation. If you have to calculate x^{y} for different values of y while x stays constant, you may be able to speed up the process creating a table with the values of x^{2-i} .

Alternatively, you can write a function to calculate e^{x} (using a table with the values of e^{2i} ), and then calculate x^{y} as e^{log(x)*y} .

update : there are also several implementations of exp(x) freely available, for instance, the one in OpenBSD is here .

Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, 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:
& &
< <
> >
[ [
] ]

Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
Log In ^{?}

remember me
What's my password?
Create A New User
Chatterbox ^{?}

and the web crawler heard nothing...
How do I use this? | Other CB clients
Other Users ^{?}
Others perusing the Monastery: (3)As of 2015-08-05 01:41 GMT
Sections ^{?}
Information ^{?}
Find Nodes ^{?}
Leftovers ^{?}
Voting Booth ^{?}
The oldest computer book still on my shelves (or on my digital media) is ...

A Programming Language, 1962
Programming the 1900 series in COBOL, 1974
Algorithms + Data Structures = Programs, 1976
The C Programming Language, 1978
System V - Interface definition, 1986
Z80 Assembly Language Programming, 1979
TheSOURCE User's Manual, 1979
The C Primer, 1983
Interactive Fortran 77, A Hands on Approach, 1984
Turbo-Pascal, 1989
Perl.1 man page, 1991
Other

Results (76 votes) ,
past polls