Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_math.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 2ed34f01c81c |
| children | 71a294bd7950 |
| rev | line source |
|---|---|
| 3950 | 1 # Python test set -- math module |
| 2 # XXXX Should not do tests around zero only | |
| 3 | |
|
24442
226ba34bd998
Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents:
24364
diff
changeset
|
4 from test.test_support import TestFailed, verbose |
| 3950 | 5 |
|
4179
33f4a91f871b
Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents:
3950
diff
changeset
|
6 seps='1e-05' |
|
33f4a91f871b
Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents:
3950
diff
changeset
|
7 eps = eval(seps) |
|
33f4a91f871b
Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents:
3950
diff
changeset
|
8 print 'math module, testing with eps', seps |
| 3950 | 9 import math |
| 10 | |
| 11 def testit(name, value, expected): | |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
12 if abs(value-expected) > eps: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
13 raise TestFailed, '%s returned %f, expected %f'%\ |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
14 (name, value, expected) |
| 3950 | 15 |
| 16 print 'constants' | |
| 17 testit('pi', math.pi, 3.1415926) | |
| 18 testit('e', math.e, 2.7182818) | |
| 19 | |
| 20 print 'acos' | |
| 21 testit('acos(-1)', math.acos(-1), math.pi) | |
| 22 testit('acos(0)', math.acos(0), math.pi/2) | |
| 23 testit('acos(1)', math.acos(1), 0) | |
| 24 | |
| 25 print 'asin' | |
| 26 testit('asin(-1)', math.asin(-1), -math.pi/2) | |
| 27 testit('asin(0)', math.asin(0), 0) | |
| 28 testit('asin(1)', math.asin(1), math.pi/2) | |
| 29 | |
| 30 print 'atan' | |
| 31 testit('atan(-1)', math.atan(-1), -math.pi/4) | |
| 32 testit('atan(0)', math.atan(0), 0) | |
| 33 testit('atan(1)', math.atan(1), math.pi/4) | |
| 34 | |
| 35 print 'atan2' | |
| 36 testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) | |
| 37 testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) | |
|
17424
7c61715752be
Reverting the "unixware7" patch: atan2(0, 1) should be 0, regardless of
Tim Peters <tim.peters@gmail.com>
parents:
17419
diff
changeset
|
38 testit('atan2(0, 1)', math.atan2(0, 1), 0) |
| 3950 | 39 testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
| 40 testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2) | |
| 41 | |
| 42 print 'ceil' | |
| 43 testit('ceil(0.5)', math.ceil(0.5), 1) | |
| 44 testit('ceil(1.0)', math.ceil(1.0), 1) | |
| 45 testit('ceil(1.5)', math.ceil(1.5), 2) | |
| 46 testit('ceil(-0.5)', math.ceil(-0.5), 0) | |
| 47 testit('ceil(-1.0)', math.ceil(-1.0), -1) | |
| 48 testit('ceil(-1.5)', math.ceil(-1.5), -1) | |
| 49 | |
| 50 print 'cos' | |
| 51 testit('cos(-pi/2)', math.cos(-math.pi/2), 0) | |
| 52 testit('cos(0)', math.cos(0), 1) | |
| 53 testit('cos(pi/2)', math.cos(math.pi/2), 0) | |
| 54 testit('cos(pi)', math.cos(math.pi), -1) | |
| 55 | |
| 56 print 'cosh' | |
| 57 testit('cosh(0)', math.cosh(0), 1) | |
| 58 testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert | |
| 59 | |
|
23426
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
60 print 'degrees' |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
61 testit('degrees(pi)', math.degrees(math.pi), 180.0) |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
62 testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
63 testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
64 |
| 3950 | 65 print 'exp' |
| 66 testit('exp(-1)', math.exp(-1), 1/math.e) | |
| 67 testit('exp(0)', math.exp(0), 1) | |
| 68 testit('exp(1)', math.exp(1), math.e) | |
| 69 | |
| 70 print 'fabs' | |
| 71 testit('fabs(-1)', math.fabs(-1), 1) | |
| 72 testit('fabs(0)', math.fabs(0), 0) | |
| 73 testit('fabs(1)', math.fabs(1), 1) | |
| 74 | |
| 75 print 'floor' | |
| 76 testit('floor(0.5)', math.floor(0.5), 0) | |
| 77 testit('floor(1.0)', math.floor(1.0), 1) | |
| 78 testit('floor(1.5)', math.floor(1.5), 1) | |
| 79 testit('floor(-0.5)', math.floor(-0.5), -1) | |
| 80 testit('floor(-1.0)', math.floor(-1.0), -1) | |
| 81 testit('floor(-1.5)', math.floor(-1.5), -2) | |
| 82 | |
| 83 print 'fmod' | |
| 84 testit('fmod(10,1)', math.fmod(10,1), 0) | |
| 85 testit('fmod(10,0.5)', math.fmod(10,0.5), 0) | |
| 86 testit('fmod(10,1.5)', math.fmod(10,1.5), 1) | |
| 87 testit('fmod(-10,1)', math.fmod(-10,1), 0) | |
| 88 testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0) | |
| 89 testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1) | |
| 90 | |
| 91 print 'frexp' | |
| 92 def testfrexp(name, (mant, exp), (emant, eexp)): | |
|
15540
9d43129c8c55
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
93 if abs(mant-emant) > eps or exp != eexp: |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
94 raise TestFailed, '%s returned %s, expected %s'%\ |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
95 (name, `mant, exp`, `emant,eexp`) |
| 3950 | 96 |
| 97 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) | |
| 98 testfrexp('frexp(0)', math.frexp(0), (0, 0)) | |
| 99 testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) | |
| 100 testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) | |
| 101 | |
| 102 print 'hypot' | |
| 103 testit('hypot(0,0)', math.hypot(0,0), 0) | |
| 104 testit('hypot(3,4)', math.hypot(3,4), 5) | |
| 105 | |
| 106 print 'ldexp' | |
| 107 testit('ldexp(0,1)', math.ldexp(0,1), 0) | |
| 108 testit('ldexp(1,1)', math.ldexp(1,1), 2) | |
| 109 testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5) | |
| 110 testit('ldexp(-1,1)', math.ldexp(-1,1), -2) | |
| 111 | |
| 112 print 'log' | |
| 113 testit('log(1/e)', math.log(1/math.e), -1) | |
| 114 testit('log(1)', math.log(1), 0) | |
| 115 testit('log(e)', math.log(math.e), 1) | |
|
26661
b12e326b4ea2
Apply SF patch 652930: Add optional base argument to math.log(x[, base]).
Raymond Hettinger <python@rcn.com>
parents:
24442
diff
changeset
|
116 testit('log(32,2)', math.log(32,2), 5) |
|
26705
2ed34f01c81c
Expand log() tests to include long integers.
Raymond Hettinger <python@rcn.com>
parents:
26661
diff
changeset
|
117 testit('log(10**40, 10)', math.log(10**40, 10), 40) |
|
2ed34f01c81c
Expand log() tests to include long integers.
Raymond Hettinger <python@rcn.com>
parents:
26661
diff
changeset
|
118 testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
| 3950 | 119 |
| 120 print 'log10' | |
| 121 testit('log10(0.1)', math.log10(0.1), -1) | |
| 122 testit('log10(1)', math.log10(1), 0) | |
| 123 testit('log10(10)', math.log10(10), 1) | |
| 124 | |
| 125 print 'modf' | |
| 126 def testmodf(name, (v1, v2), (e1, e2)): | |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
127 if abs(v1-e1) > eps or abs(v2-e2): |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
128 raise TestFailed, '%s returned %s, expected %s'%\ |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
15247
diff
changeset
|
129 (name, `v1,v2`, `e1,e2`) |
| 3950 | 130 |
| 131 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) | |
| 132 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) | |
| 133 | |
| 134 print 'pow' | |
| 135 testit('pow(0,1)', math.pow(0,1), 0) | |
| 136 testit('pow(1,0)', math.pow(1,0), 1) | |
| 137 testit('pow(2,1)', math.pow(2,1), 2) | |
| 138 testit('pow(2,-1)', math.pow(2,-1), 0.5) | |
| 139 | |
|
23426
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
140 print 'radians' |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
141 testit('radians(180)', math.radians(180), math.pi) |
|
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
142 testit('radians(90)', math.radians(90), math.pi/2) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23426
diff
changeset
|
143 testit('radians(-45)', math.radians(-45), -math.pi/4) |
|
23426
1157281f00a0
Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents:
19750
diff
changeset
|
144 |
| 3950 | 145 print 'sin' |
| 146 testit('sin(0)', math.sin(0), 0) | |
| 147 testit('sin(pi/2)', math.sin(math.pi/2), 1) | |
| 148 testit('sin(-pi/2)', math.sin(-math.pi/2), -1) | |
| 149 | |
| 150 print 'sinh' | |
| 151 testit('sinh(0)', math.sinh(0), 0) | |
| 152 testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) | |
| 153 testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) | |
| 154 | |
| 155 print 'sqrt' | |
| 156 testit('sqrt(0)', math.sqrt(0), 0) | |
| 157 testit('sqrt(1)', math.sqrt(1), 1) | |
| 158 testit('sqrt(4)', math.sqrt(4), 2) | |
| 159 | |
| 160 print 'tan' | |
| 161 testit('tan(0)', math.tan(0), 0) | |
| 162 testit('tan(pi/4)', math.tan(math.pi/4), 1) | |
| 163 testit('tan(-pi/4)', math.tan(-math.pi/4), -1) | |
| 164 | |
| 165 print 'tanh' | |
| 166 testit('tanh(0)', math.tanh(0), 0) | |
| 167 testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) | |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
168 |
|
15247
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
169 # RED_FLAG 16-Oct-2000 Tim |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
170 # While 2.0 is more consistent about exceptions than previous releases, it |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
171 # still fails this part of the test on some platforms. For now, we only |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
172 # *run* test_exceptions() in verbose mode, so that this isn't normally |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
173 # tested. |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
174 |
|
15247
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
175 def test_exceptions(): |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
176 print 'exceptions' |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
177 try: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
178 x = math.exp(-1000000000) |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
179 except: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
180 # mathmodule.c is failing to weed out underflows from libm, or |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
181 # we've got an fp format with huge dynamic range |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
182 raise TestFailed("underflowing exp() should not have raised " |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
183 "an exception") |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
184 if x != 0: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
185 raise TestFailed("underflowing exp() should have returned 0") |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
186 |
|
15247
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
187 # If this fails, probably using a strict IEEE-754 conforming libm, and x |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
188 # is +Inf afterwards. But Python wants overflows detected by default. |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
189 try: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
190 x = math.exp(1000000000) |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
191 except OverflowError: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
192 pass |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
193 else: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
194 raise TestFailed("overflowing exp() didn't trigger OverflowError") |
|
15170
09c7ac188ce2
Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
195 |
|
15247
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
196 # If this fails, it could be a puzzle. One odd possibility is that |
|
19750
35b6a1d0b1e0
Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents:
17424
diff
changeset
|
197 # mathmodule.c's macros are getting confused while comparing |
|
15247
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
198 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
199 # as a result (and so raising OverflowError instead). |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
200 try: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
201 x = math.sqrt(-1.0) |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
202 except ValueError: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
203 pass |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
204 else: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
205 raise TestFailed("sqrt(-1) didn't raise ValueError") |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
206 |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
207 if verbose: |
|
dd0d199674f8
Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents:
15171
diff
changeset
|
208 test_exceptions() |
