blob: f1e51792af68d9eb5f7e421325365e17510d7d53 [file] [log] [blame]
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00001import sys
Guido van Rossume2d4dd11997-11-24 22:24:22 +00002import test_support
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00003
4def powtest(type):
Fred Drake004d5e62000-10-23 17:22:08 +00005 if type != float:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00006 print " Testing 2-argument pow() function..."
7 for i in range(-1000, 1000):
Fred Drake004d5e62000-10-23 17:22:08 +00008 if pow(type(i), 0) != 1:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00009 raise ValueError, 'pow('+str(i)+',0) != 1'
Tim Petersc54d1902000-10-06 00:36:09 +000010 if pow(type(i), 1) != type(i):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000011 raise ValueError, 'pow('+str(i)+',1) != '+str(i)
Tim Petersc54d1902000-10-06 00:36:09 +000012 if pow(type(0), 1) != type(0):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000013 raise ValueError, 'pow(0,'+str(i)+') != 0'
Tim Petersc54d1902000-10-06 00:36:09 +000014 if pow(type(1), 1) != type(1):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000015 raise ValueError, 'pow(1,'+str(i)+') != 1'
Tim Petersc54d1902000-10-06 00:36:09 +000016
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000017 for i in range(-100, 100):
Tim Petersc54d1902000-10-06 00:36:09 +000018 if pow(type(i), 3) != i*i*i:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000019 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
Fred Drake004d5e62000-10-23 17:22:08 +000020
Tim Petersc54d1902000-10-06 00:36:09 +000021 pow2 = 1
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000022 for i in range(0,31):
Tim Petersc54d1902000-10-06 00:36:09 +000023 if pow(2, i) != pow2:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000024 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
Tim Petersc54d1902000-10-06 00:36:09 +000025 if i != 30 : pow2 = pow2*2
26
27 for othertype in int, long:
28 for i in range(-10, 0) + range(1, 10):
29 ii = type(i)
30 for j in range(1, 11):
31 jj = -othertype(j)
32 try:
33 pow(ii, jj)
34 except ValueError:
35 pass # taking an int to a neg int power should fail
36 else:
37 raise ValueError, "pow(%s, %s) did not fail" % (ii, jj)
38
39 for othertype in int, long, float:
40 for i in range(1, 100):
41 zero = type(0)
42 exp = -othertype(i/10.0)
43 if exp == 0:
44 continue
45 try:
46 pow(zero, exp)
47 except ZeroDivisionError:
48 pass # taking zero to any negative exponent should fail
49 else:
50 raise ValueError, "pow(%s, %s) did not fail" % (zero, exp)
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000051
52 print " Testing 3-argument pow() function..."
53 il, ih = -20, 20
54 jl, jh = -5, 5
55 kl, kh = -10, 10
Guido van Rossume2d4dd11997-11-24 22:24:22 +000056 compare = cmp
Tim Petersc54d1902000-10-06 00:36:09 +000057 if type == float:
58 il = 1
Guido van Rossum41360a41998-03-26 19:42:58 +000059 compare = test_support.fcmp
Tim Petersc54d1902000-10-06 00:36:09 +000060 elif type == int:
61 jl = 0
62 elif type == long:
63 jl, jh = 0, 15
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000064 for i in range(il, ih+1):
Fred Drake004d5e62000-10-23 17:22:08 +000065 for j in range(jl, jh+1):
66 for k in range(kl, kh+1):
67 if k != 0:
68 if compare(pow(type(i),j,k), pow(type(i),j)% type(k)):
69 raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
70 "," +str(k)+ ") != pow(" +str(i)+ "," + \
71 str(j)+ ") % " +str(k)
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000072
73
74print 'Testing integer mode...'
75powtest(int)
76print 'Testing long integer mode...'
77powtest(long)
78print 'Testing floating point mode...'
79powtest(float)
80
81# Other tests-- not very systematic
82
83print 'The number in both columns should match.'
Fred Drakedb1bd5c1999-12-23 15:36:42 +000084print `pow(3,3) % 8`, `pow(3,3,8)`
85print `pow(3,3) % -8`, `pow(3,3,-8)`
86print `pow(3,2) % -2`, `pow(3,2,-2)`
87print `pow(-3,3) % 8`, `pow(-3,3,8)`
88print `pow(-3,3) % -8`, `pow(-3,3,-8)`
89print `pow(5,2) % -8`, `pow(5,2,-8)`
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000090print
91
Fred Drakedb1bd5c1999-12-23 15:36:42 +000092print `pow(3L,3L) % 8`, `pow(3L,3L,8)`
93print `pow(3L,3L) % -8`, `pow(3L,3L,-8)`
94print `pow(3L,2) % -2`, `pow(3L,2,-2)`
95print `pow(-3L,3L) % 8`, `pow(-3L,3L,8)`
96print `pow(-3L,3L) % -8`, `pow(-3L,3L,-8)`
97print `pow(5L,2) % -8`, `pow(5L,2,-8)`
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000098print
99
100print pow(3.0,3.0) % 8, pow(3.0,3.0,8)
101print pow(3.0,3.0) % -8, pow(3.0,3.0,-8)
102print pow(3.0,2) % -2, pow(3.0,2,-2)
103print pow(5.0,2) % -8, pow(5.0,2,-8)
104print
105
106for i in range(-10, 11):
Fred Drake004d5e62000-10-23 17:22:08 +0000107 for j in range(0, 6):
108 for k in range(-7, 11):
109 if j >= 0 and k != 0:
110 o = pow(i,j) % k
111 n = pow(i,j,k)
112 if o != n: print 'Integer mismatch:', i,j,k
Fred Drake132dce22000-12-12 23:11:42 +0000113 if j >= 0 and k != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000114 o = pow(long(i),j) % k
115 n = pow(long(i),j,k)
116 if o != n: print 'Long mismatch:', i,j,k
Fred Drake132dce22000-12-12 23:11:42 +0000117 if i >= 0 and k != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000118 o = pow(float(i),j) % k
119 n = pow(float(i),j,k)
120 if o != n: print 'Float mismatch:', i,j,k