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:
Ed Allen Smith
Date:
October 7, 2003 15:33
Subject:
Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5
Message ID:
mid+200310072233.h97MXXwR1160357@dogberry.rutgers.edu

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.)

    -Allen

-- 
Allen Smith                       http://cesario.rutgers.edu/easmith/
February 1, 2003                               Space Shuttle Columbia
Ad Astra Per Aspera                     To The Stars Through Asperity



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