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

[PATCH] Re: Oops - Can't calculate our powers

Thread Previous | Thread Next
From:
Stephen McCamant
Date:
July 17, 2003 23:26
Subject:
[PATCH] Re: Oops - Can't calculate our powers
Message ID:
16151.37638.162561.84142@syllepsis.MIT.EDU
>>>>> "MS" == Michael G Schwern <Michael> writes:

MS> On Thu, Jul 17, 2003 at 04:28:15PM -1000, Tim Jenness wrote:

TJ> -3 ** 3 != -27
TJ> -3 ** 3 == 27

MS> FWIW this bug exists in bleadperl@18374.

Here's a patch against bleadperl. The code was checking to see whether the
exponent was zero, after a loop that worked by reducing the power
until it got to zero. The base being a power of two is in fact a
separate code path. (The fact that the same problem occurs in
POSIX::pow() is a red-herring; POSIX::pow() is not a shortcut to your
libc's pow() function).

If applied, the comment in the pow.t hunk should be changed to reflect
whatever change number this is actually applied as.

 -- Stephen

--- pp.c.orig	2003-07-07 20:04:23.000000000 -0400
+++ pp.c	2003-07-18 01:44:55.000000000 -0400
@@ -972,6 +972,7 @@
 		    register unsigned int highbit = 8 * sizeof(UV);
 		    register unsigned int lowbit = 0;
 		    register unsigned int diff;
+		    bool odd_power = (power & 1);
 		    while ((diff = (highbit - lowbit) >> 1)) {
 			if (baseuv & ~((1 << (lowbit + diff)) - 1))
 			    lowbit += diff;
@@ -994,7 +995,7 @@
 			    }
 			}
 			SP--;
-			if (baseuok || !(power & 1))
+			if (baseuok || !odd_power)
 			    /* answer is positive */
 			    SETu( result );
 			else if (result <= (UV)IV_MAX)
--- t/op/pow.t.orig	2003-07-07 20:04:12.000000000 -0400
+++ t/op/pow.t	2003-07-18 02:09:20.000000000 -0400
@@ -16,7 +16,10 @@
 my $tests;
 $tests += $_->[1] foreach @pow;
 
-plan tests => 1 + $bits_in_uv + $tests;
+plan tests => 2 + $bits_in_uv + $tests;
+
+# This gave positive 27 before change #20167
+is((-3)**3, -27, "(negative int) ** (odd power) is negative");
 
 # Ought to be 32, 64, 36 or something like that.
 

Thread Previous | Thread Next


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