1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
require 'test_helper'
class GSLTest < GSL::TestCase
def test_expm1
y = GSL.expm1(0.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.expm1(0.0)'
y = GSL.expm1(1e-10)
y_expected = 1.000000000050000000002e-10
assert_rel y, y_expected, 1e-15, 'GSL.expm1(1e-10)'
y = GSL.expm1(-1e-10)
y_expected = -9.999999999500000000017e-11
assert_rel y, y_expected, 1e-15, 'GSL.expm1(-1e-10)'
y = GSL.expm1(0.1)
y_expected = 0.1051709180756476248117078264902
assert_rel y, y_expected, 1e-15, 'GSL.expm1(0.1)'
y = GSL.expm1(-0.1)
y_expected = -0.09516258196404042683575094055356
assert_rel y, y_expected, 1e-15, 'GSL.expm1(-0.1)'
y = GSL.expm1(10.0)
y_expected = 22025.465794806716516957900645284
assert_rel y, y_expected, 1e-15, 'GSL.expm1(10.0)'
y = GSL.expm1(-10.0)
y_expected = -0.99995460007023751514846440848444
assert_rel y, y_expected, 1e-15, 'GSL.expm1(-10.0)'
end
def test_log1p
y = GSL.log1p(0.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.log1p(0.0)'
y = GSL.log1p(1e-10)
y_expected = 9.9999999995000000000333333333308e-11
assert_rel y, y_expected, 1e-15, 'GSL.log1p(1e-10)'
y = GSL.log1p(0.1)
y_expected = 0.095310179804324860043952123280765
assert_rel y, y_expected, 1e-15, 'GSL.log1p(0.1)'
y = GSL.log1p(10.0)
y_expected = 2.3978952727983705440619435779651
assert_rel y, y_expected, 1e-15, 'GSL.log1p(10.0)'
end
def test_hypot
y = GSL.hypot(0.0, 0.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.hypot(0.0, 0.0)'
y = GSL.hypot(1e-10, 1e-10)
y_expected = 1.414213562373095048801688e-10
assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e-10, 1e-10)'
y = GSL.hypot(1e-38, 1e-38)
y_expected = 1.414213562373095048801688e-38
assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e-38, 1e-38)'
y = GSL.hypot(1e-10, -1.0)
y_expected = 1.000000000000000000005
assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e-10, -1)'
y = GSL.hypot(-1.0, 1e-10)
y_expected = 1.000000000000000000005
assert_rel y, y_expected, 1e-15, 'GSL.hypot(-1, 1e-10)'
#y = GSL.hypot(1e307, 1e301)
#y_expected = 1.000000000000499999999999e307
#assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e307, 1e301)'
#y = GSL.hypot(1e301, 1e307)
#y_expected = 1.000000000000499999999999e307
#assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e301, 1e307)'
#y = GSL.hypot(1e307, 1e307)
#y_expected = 1.414213562373095048801688e307
#assert_rel y, y_expected, 1e-15, 'GSL.hypot(1e307, 1e307)'
end
def test_acosh
y = GSL.acosh(1.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.acosh(1.0)'
y = GSL.acosh(1.1)
y_expected = 4.435682543851151891329110663525e-1
assert_rel y, y_expected, 1e-15, 'GSL.acosh(1.1)'
y = GSL.acosh(10.0)
y_expected = 2.9932228461263808979126677137742e0
assert_rel y, y_expected, 1e-15, 'GSL.acosh(10.0)'
y = GSL.acosh(1e10)
y_expected = 2.3718998110500402149594646668302e1
assert_rel y, y_expected, 1e-15, 'GSL.acosh(1e10)'
end
def test_asinh
y = GSL.asinh(0.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.asinh(0.0)'
y = GSL.asinh(1e-10)
y_expected = 9.9999999999999999999833333333346e-11
assert_rel y, y_expected, 1e-15, 'GSL.asinh(1e-10)'
y = GSL.asinh(-1e-10)
y_expected = -9.9999999999999999999833333333346e-11
assert_rel y, y_expected, 1e-15, 'GSL.asinh(1e-10)'
y = GSL.asinh(0.1)
y_expected = 9.983407889920756332730312470477e-2
assert_rel y, y_expected, 1e-15, 'GSL.asinh(0.1)'
y = GSL.asinh(-0.1)
y_expected = -9.983407889920756332730312470477e-2
assert_rel y, y_expected, 1e-15, 'GSL.asinh(-0.1)'
y = GSL.asinh(1.0)
y_expected = 8.8137358701954302523260932497979e-1
assert_rel y, y_expected, 1e-15, 'GSL.asinh(1.0)'
y = GSL.asinh(-1.0)
y_expected = -8.8137358701954302523260932497979e-1
assert_rel y, y_expected, 1e-15, 'GSL.asinh(-1.0)'
y = GSL.asinh(10.0)
y_expected = 2.9982229502979697388465955375965e0
assert_rel y, y_expected, 1e-15, 'GSL.asinh(10)'
y = GSL.asinh(-10.0)
y_expected = -2.9982229502979697388465955375965e0
assert_rel y, y_expected, 1e-15, 'GSL.asinh(-10)'
y = GSL.asinh(1e10)
y_expected = 2.3718998110500402149599646668302e1
assert_rel y, y_expected, 1e-15, 'GSL.asinh(1e10)'
y = GSL.asinh(-1e10)
y_expected = -2.3718998110500402149599646668302e1
assert_rel y, y_expected, 1e-15, 'GSL.asinh(-1e10)'
end
def test_atanh
y = GSL.atanh(0.0)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.atanh(0.0)'
y = GSL.atanh(1e-20)
y_expected = 1e-20
assert_rel y, y_expected, 1e-15, 'GSL.atanh(1e-20)'
y = GSL.atanh(-1e-20)
y_expected = -1e-20
assert_rel y, y_expected, 1e-15, 'GSL.atanh(-1e-20)'
y = GSL.atanh(0.1)
y_expected = 1.0033534773107558063572655206004e-1
assert_rel y, y_expected, 1e-15, 'GSL.atanh(0.1)'
y = GSL.atanh(-0.1)
y_expected = -1.0033534773107558063572655206004e-1
assert_rel y, y_expected, 1e-15, 'GSL.atanh(-0.1)'
y = GSL.atanh(0.9)
y_expected = 1.4722194895832202300045137159439e0
assert_rel y, y_expected, 1e-15, 'GSL.atanh(0.9)'
y = GSL.atanh(-0.9)
y_expected = -1.4722194895832202300045137159439e0
assert_rel y, y_expected, 1e-15, 'GSL.atanh(0.9)'
end
def test_pow_int
y = GSL.pow_2(-3.14)
y_expected = GSL.pow(-3.14, 2.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_2(-3.14)'
y = GSL.pow_3(-3.14)
y_expected = GSL.pow(-3.14, 3.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_3(-3.14)'
y = GSL.pow_4(-3.14)
y_expected = GSL.pow(-3.14, 4.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_4(-3.14)'
y = GSL.pow_5(-3.14)
y_expected = GSL.pow(-3.14, 5.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_5(-3.14)'
y = GSL.pow_6(-3.14)
y_expected = GSL.pow(-3.14, 6.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_6(-3.14)'
y = GSL.pow_7(-3.14)
y_expected = GSL.pow(-3.14, 7.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_7(-3.14)'
y = GSL.pow_8(-3.14)
y_expected = GSL.pow(-3.14, 8.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_8(-3.14)'
y = GSL.pow_9(-3.14)
y_expected = GSL.pow(-3.14, 9.0)
assert_rel y, y_expected, 1e-15, 'GSL.pow_9(-3.14)'
-9.upto(9) { |n|
y = GSL.pow_int(-3.14, n)
y_expected = GSL.pow(-3.14, n)
assert_rel y, y_expected, 1e-15, "GSL.pow_n(-3.14,#{n})"
}
end
def test_ldexp
y = GSL.ldexp(GSL::M_PI, -2)
y_expected = GSL::M_PI_4
assert_rel y, y_expected, 1e-15, 'GSL.ldexp(pi,-2)'
y = GSL.ldexp(1.0, 2)
y_expected = 4.000000
assert_rel y, y_expected, 1e-15, 'GSL.ldexp(1.0,2)'
y = GSL.ldexp(0.0, 2)
y_expected = 0.0
assert_rel y, y_expected, 1e-15, 'GSL.ldexp(0.0,2)'
end
def test_frexp
y, e = GSL.frexp(GSL::M_PI)
y_expected = GSL::M_PI_4
e_expected = 2
assert_rel y, y_expected, 1e-15, 'GSL.frexp(pi) fraction'
assert_int e, e_expected, 'GSL.frexp(pi) exponent'
y, e = GSL.frexp(2.0)
y_expected = 0.5
e_expected = 2
assert_rel y, y_expected, 1e-15, 'GSL.frexp(2.0) fraction'
assert_int e, e_expected, 'GSL.frexp(2.0) exponent'
y, e = GSL.frexp(1.0 / 4.0)
y_expected = 0.5
e_expected = -1
assert_rel y, y_expected, 1e-15, 'GSL.frexp(0.25) fraction'
assert_int e, e_expected, 'GSL.frexp(0.25) exponent'
y, e = GSL.frexp(1.0 / 4.0 - 4.0 * GSL::DBL_EPSILON)
y_expected = 0.999999999999996447
e_expected = -2
assert_rel y, y_expected, 1e-15, 'GSL.frexp(0.25-eps) fraction'
assert_int e, e_expected, 'GSL.frexp(0.25-eps) exponent'
end
def test_gsl
x = GSL::M_PI
y = 22.0 / 7.0
10.times { |i|
tol = GSL.pow(10, -i)
res = GSL.fcmp(x, y, tol)
assert_int res, i >= 4 ? -1 : 0, "GSL.fcmp(#{x},#{y},#{tol})"
res = GSL.fcmp(y, x, tol)
assert_int res, i >= 4 ? 1 : 0, "GSL.fcmp(#{y},#{x},#{tol})"
}
zero = 0.0
one = 1.0
inf = Math.exp(1.0e10)
nan = inf / inf
s = GSL.isinf(zero)
assert_int s, 0, 'GSL.isinf(0)'
s = GSL.isinf(one)
assert_int s, 0, 'GSL.isinf(1)'
s = GSL.isinf(inf)
assert_int s, 1, 'GSL.isinf(inf)'
# Commented out 2008/Oct/17 by YT
# This test fails in (Darwin 9.5.0, gcc4.0.1):
# gsl_isinf() returns 1 for -inf
#s = GSL.isinf(-inf)
#assert_int s, -1, 'GSL.isinf(-inf)'
s = GSL.isinf(nan)
assert_int s, 0, 'GSL.isinf(nan)'
s = GSL.isnan(zero)
assert_int s, 0, 'GSL.isnan(0)'
s = GSL.isnan(one)
assert_int s, 0, 'GSL.isnan(1)'
s = GSL.isnan(inf)
assert_int s, 0, 'GSL.isnan(inf)'
s = GSL.isnan(nan)
assert_int s, 1, 'GSL.isnan(nan)'
s = GSL.finite(zero)
assert_int s, 1, 'GSL.finite(0)'
s = GSL.finite(one)
assert_int s, 1, 'GSL.finite(1)'
s = GSL.finite(inf)
assert_int s, 0, 'GSL.finite(inf)'
s = GSL.finite(nan)
assert_int s, 0, 'GSL.finite(nan)'
end
end
|