- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-math/test/math.rb
r321 r439 2 2 # Math Test 3 3 4 ## 5 # Performs fuzzy check for equality on methods returning floats 6 # on the basis of the Math::TOLERANCE constant. 7 def 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 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) 15 9 end 16 10 end 17 11 18 12 assert('Math.sin 0') do 19 check_float(Math.sin(0), 0)13 assert_float(0, Math.sin(0)) 20 14 end 21 15 22 16 assert('Math.sin PI/2') do 23 check_float(Math.sin(Math::PI / 2), 1)17 assert_float(1, Math.sin(Math::PI / 2)) 24 18 end 25 19 26 20 assert('Math.cos 0') do 27 check_float(Math.cos(0), 1)21 assert_float(1, Math.cos(0)) 28 22 end 29 23 30 24 assert('Math.cos PI/2') do 31 check_float(Math.cos(Math::PI / 2), 0)25 assert_float(0, Math.cos(Math::PI / 2)) 32 26 end 33 27 34 28 assert('Math.tan 0') do 35 check_float(Math.tan(0), 0)29 assert_float(0, Math.tan(0)) 36 30 end 37 31 38 32 assert('Math.tan PI/4') do 39 check_float(Math.tan(Math::PI / 4), 1)33 assert_float(1, Math.tan(Math::PI / 4)) 40 34 end 41 35 42 36 assert('Fundamental trig identities') do 43 result = true44 37 N = 13 45 38 N.times do |i| … … 49 42 c = Math.cos(a) 50 43 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 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 58 50 end 59 51 60 52 assert('Math.erf 0') do 61 check_float(Math.erf(0), 0)53 assert_float(0, Math.erf(0)) 62 54 end 63 55 64 56 assert('Math.exp 0') do 65 check_float(Math.exp(0), 1.0)57 assert_float(1.0, Math.exp(0)) 66 58 end 67 59 68 60 assert('Math.exp 1') do 69 check_float(Math.exp(1), 2.718281828459045)61 assert_float(2.718281828459045, Math.exp(1)) 70 62 end 71 63 72 64 assert('Math.exp 1.5') do 73 check_float(Math.exp(1.5), 4.4816890703380645)65 assert_float(4.4816890703380645, Math.exp(1.5)) 74 66 end 75 67 76 68 assert('Math.log 1') do 77 check_float(Math.log(1), 0)69 assert_float(0, Math.log(1)) 78 70 end 79 71 80 72 assert('Math.log E') do 81 check_float(Math.log(Math::E), 1.0)73 assert_float(1.0, Math.log(Math::E)) 82 74 end 83 75 84 76 assert('Math.log E**3') do 85 check_float(Math.log(Math::E**3), 3.0)77 assert_float(3.0, Math.log(Math::E**3)) 86 78 end 87 79 88 80 assert('Math.log2 1') do 89 check_float(Math.log2(1), 0.0)81 assert_float(0.0, Math.log2(1)) 90 82 end 91 83 92 84 assert('Math.log2 2') do 93 check_float(Math.log2(2), 1.0)85 assert_float(1.0, Math.log2(2)) 94 86 end 95 87 96 88 assert('Math.log10 1') do 97 check_float(Math.log10(1), 0.0)89 assert_float(0.0, Math.log10(1)) 98 90 end 99 91 100 92 assert('Math.log10 10') do 101 check_float(Math.log10(10), 1.0)93 assert_float(1.0, Math.log10(10)) 102 94 end 103 95 104 96 assert('Math.log10 10**100') do 105 check_float(Math.log10(10**100), 100.0)97 assert_float(100.0, Math.log10(10**100)) 106 98 end 107 99 … … 109 101 num = [0.0, 1.0, 2.0, 3.0, 4.0] 110 102 sqr = [0, 1, 4, 9, 16] 111 result = true112 103 sqr.each_with_index do |v,i| 113 result &= check_float(Math.sqrt(v), num[i]) 114 end 115 result 104 assert_float(num[i], Math.sqrt(v)) 105 end 116 106 end 117 107 … … 119 109 num = [-2.0, -1.0, 0.0, 1.0, 2.0] 120 110 cub = [-8, -1, 0, 1, 8] 121 result = true122 111 cub.each_with_index do |v,i| 123 result &= check_float(Math.cbrt(v), num[i]) 124 end 125 result 112 assert_float(num[i], Math.cbrt(v)) 113 end 126 114 end 127 115 128 116 assert('Math.hypot') do 129 check_float(Math.hypot(3, 4), 5.0) 130 end 131 132 assert('Math.frexp 1234') do 133 n = 1234 134 fraction, exponent = Math.frexp(n) 135 check_float(Math.ldexp(fraction, exponent), n) 117 assert_float(5.0, Math.hypot(3, 4)) 136 118 end 137 119 138 120 assert('Math.erf 1') do 139 check_float(Math.erf(1), 0.842700792949715)121 assert_float(0.842700792949715, Math.erf(1)) 140 122 end 141 123 142 124 assert('Math.erfc 1') do 143 check_float(Math.erfc(1), 0.157299207050285)125 assert_float(0.157299207050285, Math.erfc(1)) 144 126 end 145 127 146 128 assert('Math.erf -1') do 147 check_float(Math.erf(-1), -0.8427007929497148)129 assert_float(-0.8427007929497148, Math.erf(-1)) 148 130 end 149 131 150 132 assert('Math.erfc -1') do 151 check_float(Math.erfc(-1), 1.8427007929497148) 152 end 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
Note:
See TracChangeset
for help on using the changeset viewer.