D
Dan van Ginhoven
Hi.
I needed a cubic root subroutine but didn´t find one at a quick search.
So I wrote a simple one.
It seems pretty fast and accurate enough.
Any ideas how to improve it,
or point me to better solutions.
/dg
#---------------------------------------------------------------------------
# Windows Perl
for ($number=2; $number < 126;$number++) {
$resp = cubicroot($number);
print "3 root of $number = $resp\n";
}
exit;
#------------------------------------------------------------------
sub cubicroot{
my ($number) = @_;
my ($reply);
$number = abs($number);
my $reply = int(sqrt(sqrt($number))); #Need to start somewhere. Any
better idea ?
my ($diff,$decimal,$fraction);
# first find the nearest higher whole number
while ($reply**3 < $number ) {
$reply +=1;
}
$reply -=1;
# We start with 1.0 to catch the even cubic ( 8 27 64 125 ...)
# and then count down to the nerast lower by 0.1
$decimal = 1.0;
$fraction = 0.1;
$diff = 1;
WHILE:
while ($diff > 0) {
while ( ($reply + $decimal)**3 > $number ) {
$decimal -= $fraction;
#we need a precision check here
if ($decimal - $fraction == $decimal) {last WHILE}
}
# Now we divide fraction by ten and go up again
$fraction = $fraction / 10;
while ( ($reply + $decimal)**3 < $number ) {
$decimal +=$fraction;
#we need a precision check here
if ($decimal + $fraction == $decimal) {last WHILE}
}
# Divide fraction by ten for next loop
$fraction = $fraction / 10;
# Calculate diff before next loop
$diff = abs( ($reply + $decimal)**3 - $number);
}
return $reply+$decimal;
}
I needed a cubic root subroutine but didn´t find one at a quick search.
So I wrote a simple one.
It seems pretty fast and accurate enough.
Any ideas how to improve it,
or point me to better solutions.
/dg
#---------------------------------------------------------------------------
# Windows Perl
for ($number=2; $number < 126;$number++) {
$resp = cubicroot($number);
print "3 root of $number = $resp\n";
}
exit;
#------------------------------------------------------------------
sub cubicroot{
my ($number) = @_;
my ($reply);
$number = abs($number);
my $reply = int(sqrt(sqrt($number))); #Need to start somewhere. Any
better idea ?
my ($diff,$decimal,$fraction);
# first find the nearest higher whole number
while ($reply**3 < $number ) {
$reply +=1;
}
$reply -=1;
# We start with 1.0 to catch the even cubic ( 8 27 64 125 ...)
# and then count down to the nerast lower by 0.1
$decimal = 1.0;
$fraction = 0.1;
$diff = 1;
WHILE:
while ($diff > 0) {
while ( ($reply + $decimal)**3 > $number ) {
$decimal -= $fraction;
#we need a precision check here
if ($decimal - $fraction == $decimal) {last WHILE}
}
# Now we divide fraction by ten and go up again
$fraction = $fraction / 10;
while ( ($reply + $decimal)**3 < $number ) {
$decimal +=$fraction;
#we need a precision check here
if ($decimal + $fraction == $decimal) {last WHILE}
}
# Divide fraction by ten for next loop
$fraction = $fraction / 10;
# Calculate diff before next loop
$diff = abs( ($reply + $decimal)**3 - $number);
}
return $reply+$decimal;
}