source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-math/test/math.rb@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 6.1 KB
Line 
1##
2# Math Test
3
4def 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
10end
11
12assert('Math.sin 0') do
13 assert_float(0, Math.sin(0))
14end
15
16assert('Math.sin PI/2') do
17 assert_float(1, Math.sin(Math::PI / 2))
18end
19
20assert('Math.cos 0') do
21 assert_float(1, Math.cos(0))
22end
23
24assert('Math.cos PI/2') do
25 assert_float(0, Math.cos(Math::PI / 2))
26end
27
28assert('Math.tan 0') do
29 assert_float(0, Math.tan(0))
30end
31
32assert('Math.tan PI/4') do
33 assert_float(1, Math.tan(Math::PI / 4))
34end
35
36assert('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
50end
51
52assert('Math.erf 0') do
53 assert_float(0, Math.erf(0))
54end
55
56assert('Math.exp 0') do
57 assert_float(1.0, Math.exp(0))
58end
59
60assert('Math.exp 1') do
61 assert_float(2.718281828459045, Math.exp(1))
62end
63
64assert('Math.exp 1.5') do
65 assert_float(4.4816890703380645, Math.exp(1.5))
66end
67
68assert('Math.log 1') do
69 assert_float(0, Math.log(1))
70end
71
72assert('Math.log E') do
73 assert_float(1.0, Math.log(Math::E))
74end
75
76assert('Math.log E**3') do
77 assert_float(3.0, Math.log(Math::E**3))
78end
79
80assert('Math.log2 1') do
81 assert_float(0.0, Math.log2(1))
82end
83
84assert('Math.log2 2') do
85 assert_float(1.0, Math.log2(2))
86end
87
88assert('Math.log10 1') do
89 assert_float(0.0, Math.log10(1))
90end
91
92assert('Math.log10 10') do
93 assert_float(1.0, Math.log10(10))
94end
95
96assert('Math.log10 10**100') do
97 assert_float(100.0, Math.log10(10**100))
98end
99
100assert('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
106end
107
108assert('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
114end
115
116assert('Math.hypot') do
117 assert_float(5.0, Math.hypot(3, 4))
118end
119
120assert('Math.erf 1') do
121 assert_float(0.842700792949715, Math.erf(1))
122end
123
124assert('Math.erfc 1') do
125 assert_float(0.157299207050285, Math.erfc(1))
126end
127
128assert('Math.erf -1') do
129 assert_float(-0.8427007929497148, Math.erf(-1))
130end
131
132assert('Math.erfc -1') do
133 assert_float(1.8427007929497148, Math.erfc(-1))
134end
135
136assert('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) }
143end
144
145assert('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) }
153end
154
155assert('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))
160end
161
162assert('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))
166end
167
168assert('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))
172end
173
174assert('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))
180end
181
182assert('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) }
188end
189
190assert('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))
194end
195
196assert('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) }
204end
205
206assert('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))
223end
224
225assert('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))
231end
232
233assert('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))
239end
Note: See TracBrowser for help on using the repository browser.