1 | ##
|
---|
2 | # Math Test
|
---|
3 |
|
---|
4 | def assert_float_and_int(exp_ary, act_ary)
|
---|
5 | assert('assert_float_and_int') do
|
---|
6 | flo_exp, int_exp, flo_act, int_act = *exp_ary, *act_ary
|
---|
7 | assert_float(flo_exp, flo_act)
|
---|
8 | assert_operator(int_exp, :eql?, int_act)
|
---|
9 | end
|
---|
10 | end
|
---|
11 |
|
---|
12 | assert('Math.sin 0') do
|
---|
13 | assert_float(0, Math.sin(0))
|
---|
14 | end
|
---|
15 |
|
---|
16 | assert('Math.sin PI/2') do
|
---|
17 | assert_float(1, Math.sin(Math::PI / 2))
|
---|
18 | end
|
---|
19 |
|
---|
20 | assert('Math.cos 0') do
|
---|
21 | assert_float(1, Math.cos(0))
|
---|
22 | end
|
---|
23 |
|
---|
24 | assert('Math.cos PI/2') do
|
---|
25 | assert_float(0, Math.cos(Math::PI / 2))
|
---|
26 | end
|
---|
27 |
|
---|
28 | assert('Math.tan 0') do
|
---|
29 | assert_float(0, Math.tan(0))
|
---|
30 | end
|
---|
31 |
|
---|
32 | assert('Math.tan PI/4') do
|
---|
33 | assert_float(1, Math.tan(Math::PI / 4))
|
---|
34 | end
|
---|
35 |
|
---|
36 | assert('Fundamental trig identities') do
|
---|
37 | N = 13
|
---|
38 | N.times do |i|
|
---|
39 | a = Math::PI / N * i
|
---|
40 | ca = Math::PI / 2 - a
|
---|
41 | s = Math.sin(a)
|
---|
42 | c = Math.cos(a)
|
---|
43 | t = Math.tan(a)
|
---|
44 | assert_float(Math.cos(ca), s)
|
---|
45 | assert_float(1 / Math.tan(ca), t)
|
---|
46 | assert_float(1, s ** 2 + c ** 2)
|
---|
47 | assert_float((1/c) ** 2, t ** 2 + 1)
|
---|
48 | assert_float((1/s) ** 2, (1/t) ** 2 + 1)
|
---|
49 | end
|
---|
50 | end
|
---|
51 |
|
---|
52 | assert('Math.erf 0') do
|
---|
53 | assert_float(0, Math.erf(0))
|
---|
54 | end
|
---|
55 |
|
---|
56 | assert('Math.exp 0') do
|
---|
57 | assert_float(1.0, Math.exp(0))
|
---|
58 | end
|
---|
59 |
|
---|
60 | assert('Math.exp 1') do
|
---|
61 | assert_float(2.718281828459045, Math.exp(1))
|
---|
62 | end
|
---|
63 |
|
---|
64 | assert('Math.exp 1.5') do
|
---|
65 | assert_float(4.4816890703380645, Math.exp(1.5))
|
---|
66 | end
|
---|
67 |
|
---|
68 | assert('Math.log 1') do
|
---|
69 | assert_float(0, Math.log(1))
|
---|
70 | end
|
---|
71 |
|
---|
72 | assert('Math.log E') do
|
---|
73 | assert_float(1.0, Math.log(Math::E))
|
---|
74 | end
|
---|
75 |
|
---|
76 | assert('Math.log E**3') do
|
---|
77 | assert_float(3.0, Math.log(Math::E**3))
|
---|
78 | end
|
---|
79 |
|
---|
80 | assert('Math.log2 1') do
|
---|
81 | assert_float(0.0, Math.log2(1))
|
---|
82 | end
|
---|
83 |
|
---|
84 | assert('Math.log2 2') do
|
---|
85 | assert_float(1.0, Math.log2(2))
|
---|
86 | end
|
---|
87 |
|
---|
88 | assert('Math.log10 1') do
|
---|
89 | assert_float(0.0, Math.log10(1))
|
---|
90 | end
|
---|
91 |
|
---|
92 | assert('Math.log10 10') do
|
---|
93 | assert_float(1.0, Math.log10(10))
|
---|
94 | end
|
---|
95 |
|
---|
96 | assert('Math.log10 10**100') do
|
---|
97 | assert_float(100.0, Math.log10(10**100))
|
---|
98 | end
|
---|
99 |
|
---|
100 | assert('Math.sqrt') do
|
---|
101 | num = [0.0, 1.0, 2.0, 3.0, 4.0]
|
---|
102 | sqr = [0, 1, 4, 9, 16]
|
---|
103 | sqr.each_with_index do |v,i|
|
---|
104 | assert_float(num[i], Math.sqrt(v))
|
---|
105 | end
|
---|
106 | end
|
---|
107 |
|
---|
108 | assert('Math.cbrt') do
|
---|
109 | num = [-2.0, -1.0, 0.0, 1.0, 2.0]
|
---|
110 | cub = [-8, -1, 0, 1, 8]
|
---|
111 | cub.each_with_index do |v,i|
|
---|
112 | assert_float(num[i], Math.cbrt(v))
|
---|
113 | end
|
---|
114 | end
|
---|
115 |
|
---|
116 | assert('Math.hypot') do
|
---|
117 | assert_float(5.0, Math.hypot(3, 4))
|
---|
118 | end
|
---|
119 |
|
---|
120 | assert('Math.erf 1') do
|
---|
121 | assert_float(0.842700792949715, Math.erf(1))
|
---|
122 | end
|
---|
123 |
|
---|
124 | assert('Math.erfc 1') do
|
---|
125 | assert_float(0.157299207050285, Math.erfc(1))
|
---|
126 | end
|
---|
127 |
|
---|
128 | assert('Math.erf -1') do
|
---|
129 | assert_float(-0.8427007929497148, Math.erf(-1))
|
---|
130 | end
|
---|
131 |
|
---|
132 | assert('Math.erfc -1') do
|
---|
133 | assert_float(1.8427007929497148, Math.erfc(-1))
|
---|
134 | end
|
---|
135 |
|
---|
136 | assert('Math.acos') do
|
---|
137 | assert_float(0 * Math::PI / 4, Math.acos( 1.0))
|
---|
138 | assert_float(1 * Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
|
---|
139 | assert_float(2 * Math::PI / 4, Math.acos( 0.0))
|
---|
140 | assert_float(4 * Math::PI / 4, Math.acos(-1.0))
|
---|
141 | assert_raise(Math::DomainError) { Math.acos(+1.1) }
|
---|
142 | assert_raise(Math::DomainError) { Math.acos(-1.1) }
|
---|
143 | end
|
---|
144 |
|
---|
145 | assert('Math.asin') do
|
---|
146 | assert_float( 0 * Math::PI / 4, Math.asin( 0.0))
|
---|
147 | assert_float( 1 * Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
|
---|
148 | assert_float( 2 * Math::PI / 4, Math.asin( 1.0))
|
---|
149 | assert_float(-2 * Math::PI / 4, Math.asin(-1.0))
|
---|
150 | assert_raise(Math::DomainError) { Math.asin(+1.1) }
|
---|
151 | assert_raise(Math::DomainError) { Math.asin(-1.1) }
|
---|
152 | assert_raise(Math::DomainError) { Math.asin(2.0) }
|
---|
153 | end
|
---|
154 |
|
---|
155 | assert('Math.atan') do
|
---|
156 | assert_float( 0 * Math::PI / 4, Math.atan( 0.0))
|
---|
157 | assert_float( 1 * Math::PI / 4, Math.atan( 1.0))
|
---|
158 | assert_float( 2 * Math::PI / 4, Math.atan(1.0 / 0.0))
|
---|
159 | assert_float(-1 * Math::PI / 4, Math.atan(-1.0))
|
---|
160 | end
|
---|
161 |
|
---|
162 | assert('Math.cosh') do
|
---|
163 | assert_float(1, Math.cosh(0))
|
---|
164 | assert_float((Math::E ** 1 + Math::E ** -1) / 2, Math.cosh(1))
|
---|
165 | assert_float((Math::E ** 2 + Math::E ** -2) / 2, Math.cosh(2))
|
---|
166 | end
|
---|
167 |
|
---|
168 | assert('Math.sinh') do
|
---|
169 | assert_float(0, Math.sinh(0))
|
---|
170 | assert_float((Math::E ** 1 - Math::E ** -1) / 2, Math.sinh(1))
|
---|
171 | assert_float((Math::E ** 2 - Math::E ** -2) / 2, Math.sinh(2))
|
---|
172 | end
|
---|
173 |
|
---|
174 | assert('Math.tanh') do
|
---|
175 | assert_float(Math.sinh(0) / Math.cosh(0), Math.tanh(0))
|
---|
176 | assert_float(Math.sinh(1) / Math.cosh(1), Math.tanh(1))
|
---|
177 | assert_float(Math.sinh(2) / Math.cosh(2), Math.tanh(2))
|
---|
178 | assert_float(+1.0, Math.tanh(+1000.0))
|
---|
179 | assert_float(-1.0, Math.tanh(-1000.0))
|
---|
180 | end
|
---|
181 |
|
---|
182 | assert('Math.acosh') do
|
---|
183 | assert_float(0, Math.acosh(1))
|
---|
184 | assert_float(1, Math.acosh((Math::E ** 1 + Math::E ** -1) / 2))
|
---|
185 | assert_float(2, Math.acosh((Math::E ** 2 + Math::E ** -2) / 2))
|
---|
186 | assert_raise(Math::DomainError) { Math.acosh(0.9) }
|
---|
187 | assert_raise(Math::DomainError) { Math.acosh(0) }
|
---|
188 | end
|
---|
189 |
|
---|
190 | assert('Math.asinh') do
|
---|
191 | assert_float(0, Math.asinh(0))
|
---|
192 | assert_float(1, Math.asinh((Math::E ** 1 - Math::E ** -1) / 2))
|
---|
193 | assert_float(2, Math.asinh((Math::E ** 2 - Math::E ** -2) / 2))
|
---|
194 | end
|
---|
195 |
|
---|
196 | assert('Math.atanh') do
|
---|
197 | assert_float(0, Math.atanh(Math.sinh(0) / Math.cosh(0)))
|
---|
198 | assert_float(1, Math.atanh(Math.sinh(1) / Math.cosh(1)))
|
---|
199 | assert_float(2, Math.atanh(Math.sinh(2) / Math.cosh(2)))
|
---|
200 | assert_float(Float::INFINITY, Math.atanh(1))
|
---|
201 | assert_float(-Float::INFINITY, Math.atanh(-1))
|
---|
202 | assert_raise(Math::DomainError) { Math.atanh(+1.1) }
|
---|
203 | assert_raise(Math::DomainError) { Math.atanh(-1.1) }
|
---|
204 | end
|
---|
205 |
|
---|
206 | assert('Math.atan2') do
|
---|
207 | assert_float(+0.0, Math.atan2(+0.0, +0.0))
|
---|
208 | assert_float(-0.0, Math.atan2(-0.0, +0.0))
|
---|
209 | assert_float(+Math::PI, Math.atan2(+0.0, -0.0))
|
---|
210 | assert_float(-Math::PI, Math.atan2(-0.0, -0.0))
|
---|
211 |
|
---|
212 | inf = Float::INFINITY
|
---|
213 | expected = 3.0 * Math::PI / 4.0
|
---|
214 | assert_float(+expected, Math.atan2(+inf, -inf))
|
---|
215 | assert_float(-expected, Math.atan2(-inf, -inf))
|
---|
216 | expected = Math::PI / 4.0
|
---|
217 | assert_float(+expected, Math.atan2(+inf, +inf))
|
---|
218 | assert_float(-expected, Math.atan2(-inf, +inf))
|
---|
219 |
|
---|
220 | assert_float(0, Math.atan2(0, 1))
|
---|
221 | assert_float(Math::PI / 4, Math.atan2(1, 1))
|
---|
222 | assert_float(Math::PI / 2, Math.atan2(1, 0))
|
---|
223 | end
|
---|
224 |
|
---|
225 | assert('Math.ldexp') do
|
---|
226 | assert_float(0.0, Math.ldexp(0.0, 0.0))
|
---|
227 | assert_float(0.5, Math.ldexp(0.5, 0.0))
|
---|
228 | assert_float(1.0, Math.ldexp(0.5, 1.0))
|
---|
229 | assert_float(2.0, Math.ldexp(0.5, 2.0))
|
---|
230 | assert_float(3.0, Math.ldexp(0.75, 2.0))
|
---|
231 | end
|
---|
232 |
|
---|
233 | assert('Math.frexp') do
|
---|
234 | assert_float_and_int([0.0, 0], Math.frexp(0.0))
|
---|
235 | assert_float_and_int([0.5, 0], Math.frexp(0.5))
|
---|
236 | assert_float_and_int([0.5, 1], Math.frexp(1.0))
|
---|
237 | assert_float_and_int([0.5, 2], Math.frexp(2.0))
|
---|
238 | assert_float_and_int([0.75, 2], Math.frexp(3.0))
|
---|
239 | end
|
---|