develooper Front page | perl.perl5.porters | Postings from October 2003

Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5

From:
Yitzchak Scott-Thoennes
Date:
October 7, 2003 15:56
Subject:
Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5
Message ID:
20031007225502.GA732@efn.org
On Tue, Oct 07, 2003 at 06:33:33PM -0400, Ed Allen Smith <easmith@beatrice.rutgers.edu> wrote:
> 
> I'll change my earlier suggestion of how to handle this in Math::BigInt. I
> suggest changing the current code in _root from:
> 
>       # fit's into one Perl scalar, so result can be computed directly
>       $x->[0] = int( $x->[0] ** (1 / $n->[0]) );
> 
> to:
> 
> 	if ($x->[0] == 0) {
> 	   return 0;
> 	}
> 	# fits into one Perl scalar, so result can be computed directly
> 	my $temp = $x->[0] ** (1 / $n->[0]);
> 	my $a = int($temp);
> 	my $b = int($temp + (($temp > 0) ? 0.5 : -0.5));
> 	if ($a == $b) {
> 	   $x->[0] = $a;
> 	} elsif (abs(($a**$n->[0]) - $x->[0]) > abs(($b**$n->[0]) - $x->[0])) {
> 	   $x->[0] = $b;
> 	} else {
> 	   $x->[0] = $a;
> 	}
> 	return $x;
> 
> In other words, if adding 0.5 (equivalently, subtracting, if the result is
> negative) will give a more arithmetically correct result after truncating, do
> it; otherwise, don't. (It's likely that the same sort of thing would be
> helpful for the _sqrt function.)

That's basically just a form of rounding, only not based on just
rounding the result.  It looks more to me as if the goal here is to
come up with the greatest result $a such that $a**$n->[0] <= $x->[0]
(not sure if that "greatest" or "<=" is meant to apply to absolute
values or not).

But your general method certainly works for that.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About