blob: af84d2a614d55d9416da66bf08894ee3ba95ae48 [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
6eps=1e-5
7print 'math module, testing with eps', eps
8import math
9
10def testit(name, value, expected):
11 if abs(value-expected) > eps:
12 raise TestFailed, '%s returned %f, expected %f'%\
13 (name, value, expected)
14
15print 'constants'
16testit('pi', math.pi, 3.1415926)
17testit('e', math.e, 2.7182818)
18
19print 'acos'
20testit('acos(-1)', math.acos(-1), math.pi)
21testit('acos(0)', math.acos(0), math.pi/2)
22testit('acos(1)', math.acos(1), 0)
23
24print 'asin'
25testit('asin(-1)', math.asin(-1), -math.pi/2)
26testit('asin(0)', math.asin(0), 0)
27testit('asin(1)', math.asin(1), math.pi/2)
28
29print 'atan'
30testit('atan(-1)', math.atan(-1), -math.pi/4)
31testit('atan(0)', math.atan(0), 0)
32testit('atan(1)', math.atan(1), math.pi/4)
33
34print 'atan2'
35testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
36testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
37testit('atan2(0, 1)', math.atan2(0, 1), 0)
38testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
39testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
40
41print 'ceil'
42testit('ceil(0.5)', math.ceil(0.5), 1)
43testit('ceil(1.0)', math.ceil(1.0), 1)
44testit('ceil(1.5)', math.ceil(1.5), 2)
45testit('ceil(-0.5)', math.ceil(-0.5), 0)
46testit('ceil(-1.0)', math.ceil(-1.0), -1)
47testit('ceil(-1.5)', math.ceil(-1.5), -1)
48
49print 'cos'
50testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
51testit('cos(0)', math.cos(0), 1)
52testit('cos(pi/2)', math.cos(math.pi/2), 0)
53testit('cos(pi)', math.cos(math.pi), -1)
54
55print 'cosh'
56testit('cosh(0)', math.cosh(0), 1)
57testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
58
59print 'exp'
60testit('exp(-1)', math.exp(-1), 1/math.e)
61testit('exp(0)', math.exp(0), 1)
62testit('exp(1)', math.exp(1), math.e)
63
64print 'fabs'
65testit('fabs(-1)', math.fabs(-1), 1)
66testit('fabs(0)', math.fabs(0), 0)
67testit('fabs(1)', math.fabs(1), 1)
68
69print 'floor'
70testit('floor(0.5)', math.floor(0.5), 0)
71testit('floor(1.0)', math.floor(1.0), 1)
72testit('floor(1.5)', math.floor(1.5), 1)
73testit('floor(-0.5)', math.floor(-0.5), -1)
74testit('floor(-1.0)', math.floor(-1.0), -1)
75testit('floor(-1.5)', math.floor(-1.5), -2)
76
77print 'fmod'
78testit('fmod(10,1)', math.fmod(10,1), 0)
79testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
80testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
81testit('fmod(-10,1)', math.fmod(-10,1), 0)
82testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
83testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
84
85print 'frexp'
86def testfrexp(name, (mant, exp), (emant, eexp)):
87 if abs(mant-emant) > eps or exp <> eexp:
88 raise TestFailed, '%s returned %s, expected %s'%\
89 (name, `mant, exp`, `emant,eexp`)
90
91testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
92testfrexp('frexp(0)', math.frexp(0), (0, 0))
93testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
94testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
95
96print 'hypot'
97testit('hypot(0,0)', math.hypot(0,0), 0)
98testit('hypot(3,4)', math.hypot(3,4), 5)
99
100print 'ldexp'
101testit('ldexp(0,1)', math.ldexp(0,1), 0)
102testit('ldexp(1,1)', math.ldexp(1,1), 2)
103testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
104testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
105
106print 'log'
107testit('log(1/e)', math.log(1/math.e), -1)
108testit('log(1)', math.log(1), 0)
109testit('log(e)', math.log(math.e), 1)
110
111print 'log10'
112testit('log10(0.1)', math.log10(0.1), -1)
113testit('log10(1)', math.log10(1), 0)
114testit('log10(10)', math.log10(10), 1)
115
116print 'modf'
117def testmodf(name, (v1, v2), (e1, e2)):
118 if abs(v1-e1) > eps or abs(v2-e2):
119 raise TestFailed, '%s returned %s, expected %s'%\
120 (name, `v1,v2`, `e1,e2`)
121
122testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
123testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
124
125print 'pow'
126testit('pow(0,1)', math.pow(0,1), 0)
127testit('pow(1,0)', math.pow(1,0), 1)
128testit('pow(2,1)', math.pow(2,1), 2)
129testit('pow(2,-1)', math.pow(2,-1), 0.5)
130
131print 'sin'
132testit('sin(0)', math.sin(0), 0)
133testit('sin(pi/2)', math.sin(math.pi/2), 1)
134testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
135
136print 'sinh'
137testit('sinh(0)', math.sinh(0), 0)
138testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
139testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
140
141print 'sqrt'
142testit('sqrt(0)', math.sqrt(0), 0)
143testit('sqrt(1)', math.sqrt(1), 1)
144testit('sqrt(4)', math.sqrt(4), 2)
145
146print 'tan'
147testit('tan(0)', math.tan(0), 0)
148testit('tan(pi/4)', math.tan(math.pi/4), 1)
149testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
150
151print 'tanh'
152testit('tanh(0)', math.tanh(0), 0)
153testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)