blob: 5c8efc643122bb91e0adbe0b114be6d6b18b2945 [file] [log] [blame]
Guido van Rossumfcce6301996-08-08 18:26:25 +00001# Python test set -- math module
2# XXXX Should not do tests around zero only
3
4from test_support import *
5
Guido van Rossum5ab007b1996-08-29 19:00:46 +00006seps='1e-05'
7eps = eval(seps)
8print 'math module, testing with eps', seps
Guido van Rossumfcce6301996-08-08 18:26:25 +00009import math
10
11def testit(name, value, expected):
12 if abs(value-expected) > eps:
13 raise TestFailed, '%s returned %f, expected %f'%\
14 (name, value, expected)
15
16print 'constants'
17testit('pi', math.pi, 3.1415926)
18testit('e', math.e, 2.7182818)
19
20print 'acos'
21testit('acos(-1)', math.acos(-1), math.pi)
22testit('acos(0)', math.acos(0), math.pi/2)
23testit('acos(1)', math.acos(1), 0)
24
25print 'asin'
26testit('asin(-1)', math.asin(-1), -math.pi/2)
27testit('asin(0)', math.asin(0), 0)
28testit('asin(1)', math.asin(1), math.pi/2)
29
30print 'atan'
31testit('atan(-1)', math.atan(-1), -math.pi/4)
32testit('atan(0)', math.atan(0), 0)
33testit('atan(1)', math.atan(1), math.pi/4)
34
35print 'atan2'
36testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
37testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
38testit('atan2(0, 1)', math.atan2(0, 1), 0)
39testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
40testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
41
42print 'ceil'
43testit('ceil(0.5)', math.ceil(0.5), 1)
44testit('ceil(1.0)', math.ceil(1.0), 1)
45testit('ceil(1.5)', math.ceil(1.5), 2)
46testit('ceil(-0.5)', math.ceil(-0.5), 0)
47testit('ceil(-1.0)', math.ceil(-1.0), -1)
48testit('ceil(-1.5)', math.ceil(-1.5), -1)
49
50print 'cos'
51testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
52testit('cos(0)', math.cos(0), 1)
53testit('cos(pi/2)', math.cos(math.pi/2), 0)
54testit('cos(pi)', math.cos(math.pi), -1)
55
56print 'cosh'
57testit('cosh(0)', math.cosh(0), 1)
58testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
59
60print 'exp'
61testit('exp(-1)', math.exp(-1), 1/math.e)
62testit('exp(0)', math.exp(0), 1)
63testit('exp(1)', math.exp(1), math.e)
64
65print 'fabs'
66testit('fabs(-1)', math.fabs(-1), 1)
67testit('fabs(0)', math.fabs(0), 0)
68testit('fabs(1)', math.fabs(1), 1)
69
70print 'floor'
71testit('floor(0.5)', math.floor(0.5), 0)
72testit('floor(1.0)', math.floor(1.0), 1)
73testit('floor(1.5)', math.floor(1.5), 1)
74testit('floor(-0.5)', math.floor(-0.5), -1)
75testit('floor(-1.0)', math.floor(-1.0), -1)
76testit('floor(-1.5)', math.floor(-1.5), -2)
77
78print 'fmod'
79testit('fmod(10,1)', math.fmod(10,1), 0)
80testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
81testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
82testit('fmod(-10,1)', math.fmod(-10,1), 0)
83testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
84testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
85
86print 'frexp'
87def testfrexp(name, (mant, exp), (emant, eexp)):
88 if abs(mant-emant) > eps or exp <> eexp:
89 raise TestFailed, '%s returned %s, expected %s'%\
90 (name, `mant, exp`, `emant,eexp`)
91
92testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
93testfrexp('frexp(0)', math.frexp(0), (0, 0))
94testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
95testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
96
97print 'hypot'
98testit('hypot(0,0)', math.hypot(0,0), 0)
99testit('hypot(3,4)', math.hypot(3,4), 5)
100
101print 'ldexp'
102testit('ldexp(0,1)', math.ldexp(0,1), 0)
103testit('ldexp(1,1)', math.ldexp(1,1), 2)
104testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
105testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
106
107print 'log'
108testit('log(1/e)', math.log(1/math.e), -1)
109testit('log(1)', math.log(1), 0)
110testit('log(e)', math.log(math.e), 1)
111
112print 'log10'
113testit('log10(0.1)', math.log10(0.1), -1)
114testit('log10(1)', math.log10(1), 0)
115testit('log10(10)', math.log10(10), 1)
116
117print 'modf'
118def testmodf(name, (v1, v2), (e1, e2)):
119 if abs(v1-e1) > eps or abs(v2-e2):
120 raise TestFailed, '%s returned %s, expected %s'%\
121 (name, `v1,v2`, `e1,e2`)
122
123testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
124testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
125
126print 'pow'
127testit('pow(0,1)', math.pow(0,1), 0)
128testit('pow(1,0)', math.pow(1,0), 1)
129testit('pow(2,1)', math.pow(2,1), 2)
130testit('pow(2,-1)', math.pow(2,-1), 0.5)
131
Guido van Rossum71260b82000-05-11 18:19:42 +0000132print 'rint'
Fred Drake8eded192000-06-01 17:59:17 +0000133try:
134 math.rint
135except AttributeError:
136 # this platform does not have rint, that is fine, skip the test
137 pass
138else:
139 testit('rint(0.7)', math.rint(0.7), 1)
140 testit('rint(-0.3)', math.rint(-0.3), 0)
141 testit('rint(2.5)', math.rint(2.5), 2)
142 testit('rint(3.5)', math.rint(3.5), 4)
Guido van Rossum71260b82000-05-11 18:19:42 +0000143
Guido van Rossumfcce6301996-08-08 18:26:25 +0000144print 'sin'
145testit('sin(0)', math.sin(0), 0)
146testit('sin(pi/2)', math.sin(math.pi/2), 1)
147testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
148
149print 'sinh'
150testit('sinh(0)', math.sinh(0), 0)
151testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
152testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
153
154print 'sqrt'
155testit('sqrt(0)', math.sqrt(0), 0)
156testit('sqrt(1)', math.sqrt(1), 1)
157testit('sqrt(4)', math.sqrt(4), 2)
158
159print 'tan'
160testit('tan(0)', math.tan(0), 0)
161testit('tan(pi/4)', math.tan(math.pi/4), 1)
162testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
163
164print 'tanh'
165testit('tanh(0)', math.tanh(0), 0)
166testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)