source: EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-math/test/math.rb@ 270

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

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 2.8 KB
Line 
1##
2# Math Test
3
4##
5# Performs fuzzy check for equality on methods returning floats
6# on the basis of the Math::TOLERANCE constant.
7def check_float(a, b)
8 tolerance = Math::TOLERANCE
9 a = a.to_f
10 b = b.to_f
11 if a.finite? and b.finite?
12 (a-b).abs < tolerance
13 else
14 true
15 end
16end
17
18assert('Math.sin 0') do
19 check_float(Math.sin(0), 0)
20end
21
22assert('Math.sin PI/2') do
23 check_float(Math.sin(Math::PI / 2), 1)
24end
25
26assert('Math.cos 0') do
27 check_float(Math.cos(0), 1)
28end
29
30assert('Math.cos PI/2') do
31 check_float(Math.cos(Math::PI / 2), 0)
32end
33
34assert('Math.tan 0') do
35 check_float(Math.tan(0), 0)
36end
37
38assert('Math.tan PI/4') do
39 check_float(Math.tan(Math::PI / 4), 1)
40end
41
42assert('Fundamental trig identities') do
43 result = true
44 N = 13
45 N.times do |i|
46 a = Math::PI / N * i
47 ca = Math::PI / 2 - a
48 s = Math.sin(a)
49 c = Math.cos(a)
50 t = Math.tan(a)
51 result &= check_float(s, Math.cos(ca))
52 result &= check_float(t, 1 / Math.tan(ca))
53 result &= check_float(s ** 2 + c ** 2, 1)
54 result &= check_float(t ** 2 + 1, (1/c) ** 2)
55 result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
56 end
57 result
58end
59
60assert('Math.erf 0') do
61 check_float(Math.erf(0), 0)
62end
63
64assert('Math.exp 0') do
65 check_float(Math.exp(0), 1.0)
66end
67
68assert('Math.exp 1') do
69 check_float(Math.exp(1), 2.718281828459045)
70end
71
72assert('Math.exp 1.5') do
73 check_float(Math.exp(1.5), 4.4816890703380645)
74end
75
76assert('Math.log 1') do
77 check_float(Math.log(1), 0)
78end
79
80assert('Math.log E') do
81 check_float(Math.log(Math::E), 1.0)
82end
83
84assert('Math.log E**3') do
85 check_float(Math.log(Math::E**3), 3.0)
86end
87
88assert('Math.log2 1') do
89 check_float(Math.log2(1), 0.0)
90end
91
92assert('Math.log2 2') do
93 check_float(Math.log2(2), 1.0)
94end
95
96assert('Math.log10 1') do
97 check_float(Math.log10(1), 0.0)
98end
99
100assert('Math.log10 10') do
101 check_float(Math.log10(10), 1.0)
102end
103
104assert('Math.log10 10**100') do
105 check_float(Math.log10(10**100), 100.0)
106end
107
108assert('Math.sqrt') do
109 num = [0.0, 1.0, 2.0, 3.0, 4.0]
110 sqr = [0, 1, 4, 9, 16]
111 result = true
112 sqr.each_with_index do |v,i|
113 result &= check_float(Math.sqrt(v), num[i])
114 end
115 result
116end
117
118assert('Math.cbrt') do
119 num = [-2.0, -1.0, 0.0, 1.0, 2.0]
120 cub = [-8, -1, 0, 1, 8]
121 result = true
122 cub.each_with_index do |v,i|
123 result &= check_float(Math.cbrt(v), num[i])
124 end
125 result
126end
127
128assert('Math.hypot') do
129 check_float(Math.hypot(3, 4), 5.0)
130end
131
132assert('Math.frexp 1234') do
133 n = 1234
134 fraction, exponent = Math.frexp(n)
135 check_float(Math.ldexp(fraction, exponent), n)
136end
137
138assert('Math.erf 1') do
139 check_float(Math.erf(1), 0.842700792949715)
140end
141
142assert('Math.erfc 1') do
143 check_float(Math.erfc(1), 0.157299207050285)
144end
145
146assert('Math.erf -1') do
147 check_float(Math.erf(-1), -0.8427007929497148)
148end
149
150assert('Math.erfc -1') do
151 check_float(Math.erfc(-1), 1.8427007929497148)
152end
Note: See TracBrowser for help on using the repository browser.