3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_ieee_utils.h>
33 gsl_ieee_env_setup ();
39 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(0.0)");
41 y = gsl_expm1 (1e-10);
42 y_expected = 1.000000000050000000002e-10;
43 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(1e-10)");
45 y = gsl_expm1 (-1e-10);
46 y_expected = -9.999999999500000000017e-11;
47 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(-1e-10)");
50 y_expected = 0.1051709180756476248117078264902;
51 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(0.1)");
54 y_expected = -0.09516258196404042683575094055356;
55 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(-0.1)");
58 y_expected = 22025.465794806716516957900645284;
59 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(10.0)");
61 y = gsl_expm1 (-10.0);
62 y_expected = -0.99995460007023751514846440848444;
63 gsl_test_rel (y, y_expected, 1e-15, "gsl_expm1(-10.0)");
69 gsl_test_rel (y, y_expected, 1e-15, "gsl_log1p(0.0)");
71 y = gsl_log1p (1e-10);
72 y_expected = 9.9999999995000000000333333333308e-11;
73 gsl_test_rel (y, y_expected, 1e-15, "gsl_log1p(1e-10)");
76 y_expected = 0.095310179804324860043952123280765;
77 gsl_test_rel (y, y_expected, 1e-15, "gsl_log1p(0.1)");
80 y_expected = 2.3978952727983705440619435779651;
81 gsl_test_rel (y, y_expected, 1e-15, "gsl_log1p(10.0)");
83 /* Test for gsl_hypot */
85 y = gsl_hypot (0.0, 0.0);
87 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(0.0, 0.0)");
89 y = gsl_hypot (1e-10, 1e-10);
90 y_expected = 1.414213562373095048801688e-10;
91 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e-10, 1e-10)");
93 y = gsl_hypot (1e-38, 1e-38);
94 y_expected = 1.414213562373095048801688e-38;
95 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e-38, 1e-38)");
97 y = gsl_hypot (1e-10, -1.0);
98 y_expected = 1.000000000000000000005;
99 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e-10, -1)");
101 y = gsl_hypot (-1.0, 1e-10);
102 y_expected = 1.000000000000000000005;
103 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(-1, 1e-10)");
105 y = gsl_hypot (1e307, 1e301);
106 y_expected = 1.000000000000499999999999e307;
107 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e307, 1e301)");
109 y = gsl_hypot (1e301, 1e307);
110 y_expected = 1.000000000000499999999999e307;
111 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e301, 1e307)");
113 y = gsl_hypot (1e307, 1e307);
114 y_expected = 1.414213562373095048801688e307;
115 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot(1e307, 1e307)");
117 /* Test for gsl_hypot3 */
119 y = gsl_hypot3 (0.0, 0.0, 0.0);
121 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(0.0, 0.0, 0.0)");
123 y = gsl_hypot3 (1e-10, 1e-10, 1e-10);
124 y_expected = 1.732050807568877293527446e-10;
125 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e-10, 1e-10, 1e-10)");
127 y = gsl_hypot3 (1e-38, 1e-38, 1e-38);
128 y_expected = 1.732050807568877293527446e-38;
129 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e-38, 1e-38, 1e-38)");
131 y = gsl_hypot3 (1e-10, 1e-10, -1.0);
132 y_expected = 1.000000000000000000099;
133 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e-10, 1e-10, -1)");
135 y = gsl_hypot3 (1e-10, -1.0, 1e-10);
136 y_expected = 1.000000000000000000099;
137 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e-10, -1, 1e-10)");
139 y = gsl_hypot3 (-1.0, 1e-10, 1e-10);
140 y_expected = 1.000000000000000000099;
141 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(-1, 1e-10, 1e-10)");
143 y = gsl_hypot3 (1e307, 1e301, 1e301);
144 y_expected = 1.0000000000009999999999995e307;
145 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e307, 1e301, 1e301)");
147 y = gsl_hypot3 (1e307, 1e307, 1e307);
148 y_expected = 1.732050807568877293527446e307;
149 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e307, 1e307, 1e307)");
151 y = gsl_hypot3 (1e307, 1e-307, 1e-307);
152 y_expected = 1.0e307;
153 gsl_test_rel (y, y_expected, 1e-15, "gsl_hypot3(1e307, 1e-307, 1e-307)");
159 gsl_test_rel (y, y_expected, 1e-15, "gsl_acosh(1.0)");
162 y_expected = 4.435682543851151891329110663525e-1;
163 gsl_test_rel (y, y_expected, 1e-15, "gsl_acosh(1.1)");
165 y = gsl_acosh (10.0);
166 y_expected = 2.9932228461263808979126677137742e0;
167 gsl_test_rel (y, y_expected, 1e-15, "gsl_acosh(10.0)");
169 y = gsl_acosh (1e10);
170 y_expected = 2.3718998110500402149594646668302e1;
171 gsl_test_rel (y, y_expected, 1e-15, "gsl_acosh(1e10)");
177 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(0.0)");
179 y = gsl_asinh (1e-10);
180 y_expected = 9.9999999999999999999833333333346e-11;
181 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(1e-10)");
183 y = gsl_asinh (-1e-10);
184 y_expected = -9.9999999999999999999833333333346e-11;
185 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(1e-10)");
188 y_expected = 9.983407889920756332730312470477e-2;
189 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(0.1)");
191 y = gsl_asinh (-0.1);
192 y_expected = -9.983407889920756332730312470477e-2;
193 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(-0.1)");
196 y_expected = 8.8137358701954302523260932497979e-1;
197 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(1.0)");
199 y = gsl_asinh (-1.0);
200 y_expected = -8.8137358701954302523260932497979e-1;
201 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(-1.0)");
203 y = gsl_asinh (10.0);
204 y_expected = 2.9982229502979697388465955375965e0;
205 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(10)");
207 y = gsl_asinh (-10.0);
208 y_expected = -2.9982229502979697388465955375965e0;
209 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(-10)");
211 y = gsl_asinh (1e10);
212 y_expected = 2.3718998110500402149599646668302e1;
213 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(1e10)");
215 y = gsl_asinh (-1e10);
216 y_expected = -2.3718998110500402149599646668302e1;
217 gsl_test_rel (y, y_expected, 1e-15, "gsl_asinh(-1e10)");
223 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(0.0)");
225 y = gsl_atanh (1e-20);
227 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(1e-20)");
229 y = gsl_atanh (-1e-20);
231 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(-1e-20)");
234 y_expected = 1.0033534773107558063572655206004e-1;
235 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(0.1)");
237 y = gsl_atanh (-0.1);
238 y_expected = -1.0033534773107558063572655206004e-1;
239 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(-0.1)");
242 y_expected = 1.4722194895832202300045137159439e0;
243 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(0.9)");
245 y = gsl_atanh (-0.9);
246 y_expected = -1.4722194895832202300045137159439e0;
247 gsl_test_rel (y, y_expected, 1e-15, "gsl_atanh(0.9)");
249 /* Test for pow_int */
251 y = gsl_pow_2 (-3.14);
252 y_expected = pow (-3.14, 2.0);
253 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_2(-3.14)");
255 y = gsl_pow_3 (-3.14);
256 y_expected = pow (-3.14, 3.0);
257 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_3(-3.14)");
259 y = gsl_pow_4 (-3.14);
260 y_expected = pow (-3.14, 4.0);
261 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_4(-3.14)");
263 y = gsl_pow_5 (-3.14);
264 y_expected = pow (-3.14, 5.0);
265 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_5(-3.14)");
267 y = gsl_pow_6 (-3.14);
268 y_expected = pow (-3.14, 6.0);
269 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_6(-3.14)");
271 y = gsl_pow_7 (-3.14);
272 y_expected = pow (-3.14, 7.0);
273 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_7(-3.14)");
275 y = gsl_pow_8 (-3.14);
276 y_expected = pow (-3.14, 8.0);
277 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_8(-3.14)");
279 y = gsl_pow_9 (-3.14);
280 y_expected = pow (-3.14, 9.0);
281 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_9(-3.14)");
285 for (n = -9; n < 10; n++)
287 y = gsl_pow_int (-3.14, n);
288 y_expected = pow (-3.14, n);
289 gsl_test_rel (y, y_expected, 1e-15, "gsl_pow_n(-3.14,%d)", n);
295 y = gsl_ldexp (M_PI, -2);
297 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(pi,-2)");
299 y = gsl_ldexp (1.0, 2);
300 y_expected = 4.000000;
301 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(1.0,2)");
303 y = gsl_ldexp (0.0, 2);
305 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(0.0,2)");
307 y = gsl_ldexp (9.999999999999998890e-01, 1024);
308 y_expected = GSL_DBL_MAX;
309 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp DBL_MAX");
311 y = gsl_ldexp (1e308, -2000);
312 y_expected = 8.7098098162172166755761e-295;
313 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(1e308,-2000)");
315 y = gsl_ldexp (GSL_DBL_MIN, 2000);
316 y_expected = 2.554675596204441378334779940e294;
317 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(DBL_MIN,2000)");
319 /* Test subnormals */
323 volatile double x = GSL_DBL_MIN;
324 y_expected = 2.554675596204441378334779940e294;
329 y = gsl_ldexp (x, 2000 + i);
330 gsl_test_rel (y, y_expected, 1e-15, "gsl_ldexp(DBL_MIN/2**%d,%d)",i,2000+i);
337 y = gsl_frexp (0.0, &e);
340 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(0) fraction");
341 gsl_test_int (e, e_expected, "gsl_frexp(0) exponent");
343 y = gsl_frexp (M_PI, &e);
346 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(pi) fraction");
347 gsl_test_int (e, e_expected, "gsl_frexp(pi) exponent");
349 y = gsl_frexp (2.0, &e);
352 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(2.0) fraction");
353 gsl_test_int (e, e_expected, "gsl_frexp(2.0) exponent");
355 y = gsl_frexp (1.0 / 4.0, &e);
358 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(0.25) fraction");
359 gsl_test_int (e, e_expected, "gsl_frexp(0.25) exponent");
361 y = gsl_frexp (1.0 / 4.0 - 4.0 * GSL_DBL_EPSILON, &e);
362 y_expected = 0.999999999999996447;
364 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(0.25-eps) fraction");
365 gsl_test_int (e, e_expected, "gsl_frexp(0.25-eps) exponent");
367 y = gsl_frexp (GSL_DBL_MAX, &e);
368 y_expected = 9.999999999999998890e-01;
370 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(DBL_MAX) fraction");
371 gsl_test_int (e, e_expected, "gsl_frexp(DBL_MAX) exponent");
373 y = gsl_frexp (-GSL_DBL_MAX, &e);
374 y_expected = -9.999999999999998890e-01;
376 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(-DBL_MAX) fraction");
377 gsl_test_int (e, e_expected, "gsl_frexp(-DBL_MAX) exponent");
379 y = gsl_frexp (GSL_DBL_MIN, &e);
382 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(DBL_MIN) fraction");
383 gsl_test_int (e, e_expected, "gsl_frexp(DBL_MIN) exponent");
385 y = gsl_frexp (-GSL_DBL_MIN, &e);
388 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(-DBL_MIN) fraction");
389 gsl_test_int (e, e_expected, "gsl_frexp(-DBL_MIN) exponent");
391 /* Test subnormals */
395 volatile double x = GSL_DBL_MIN;
404 y = gsl_frexp (x, &e);
405 gsl_test_rel (y, y_expected, 1e-15, "gsl_frexp(DBL_MIN/2**%d) fraction",i);
406 gsl_test_int (e, e_expected, "gsl_frexp(DBL_MIN/2**%d) exponent", i);
411 /* Test for approximate floating point comparison */
419 /* test the basic function */
421 for (i = 0; i < 10; i++)
423 double tol = pow (10, -i);
424 int res = gsl_fcmp (x, y, tol);
425 gsl_test_int (res, -(i >= 4), "gsl_fcmp(%.5f,%.5f,%g)", x, y, tol);
428 for (i = 0; i < 10; i++)
430 double tol = pow (10, -i);
431 int res = gsl_fcmp (y, x, tol);
432 gsl_test_int (res, (i >= 4), "gsl_fcmp(%.5f,%.5f,%g)", y, x, tol);
437 #if HAVE_IEEE_COMPARISONS
438 /* Test for isinf, isnan, finite */
441 double zero, one, inf, nan;
449 s = gsl_isinf (zero);
450 gsl_test_int (s, 0, "gsl_isinf(0)");
453 gsl_test_int (s, 0, "gsl_isinf(1)");
456 gsl_test_int (s, 1, "gsl_isinf(inf)");
458 /* isinf(3): In glibc 2.01 and earlier, isinf() returns a
459 non-zero value (actually: 1) if x is an infinity (positive or
460 negative). (This is all that C99 requires.) */
462 s = gsl_isinf (-inf);
463 gsl_test (s == 0, "gsl_isinf(-inf) is non-zero");
466 gsl_test_int (s, 0, "gsl_isinf(nan)");
469 s = gsl_isnan (zero);
470 gsl_test_int (s, 0, "gsl_isnan(0)");
473 gsl_test_int (s, 0, "gsl_isnan(1)");
476 gsl_test_int (s, 0, "gsl_isnan(inf)");
478 s = gsl_isnan (-inf);
479 gsl_test_int (s, 0, "gsl_isnan(-inf)");
482 gsl_test_int (s, 1, "gsl_isnan(nan)");
485 s = gsl_finite (zero);
486 gsl_test_int (s, 1, "gsl_finite(0)");
488 s = gsl_finite (one);
489 gsl_test_int (s, 1, "gsl_finite(1)");
491 s = gsl_finite (inf);
492 gsl_test_int (s, 0, "gsl_finite(inf)");
494 s = gsl_finite (-inf);
495 gsl_test_int (s, 0, "gsl_finite(-inf)");
497 s = gsl_finite (nan);
498 gsl_test_int (s, 0, "gsl_finite(nan)");
504 double x = gsl_fdiv (2.0, 3.0);
505 gsl_test_rel (x, 2.0 / 3.0, 4 * GSL_DBL_EPSILON, "gsl_fdiv(2,3)");
509 /* Test constants in gsl_math.h */
513 gsl_test_rel (x, 1.0, 4 * GSL_DBL_EPSILON, "ln(M_E)");
517 double x=pow(2.0,M_LOG2E);
518 gsl_test_rel (x, exp(1.0), 4 * GSL_DBL_EPSILON, "2^M_LOG2E");
522 double x=pow(10.0,M_LOG10E);
523 gsl_test_rel (x, exp(1.0), 4 * GSL_DBL_EPSILON, "10^M_LOG10E");
527 double x=pow(M_SQRT2, 2.0);
528 gsl_test_rel (x, 2.0, 4 * GSL_DBL_EPSILON, "M_SQRT2^2");
532 double x=pow(M_SQRT1_2, 2.0);
533 gsl_test_rel (x, 1.0/2.0, 4 * GSL_DBL_EPSILON, "M_SQRT1_2");
537 double x=pow(M_SQRT3, 2.0);
538 gsl_test_rel (x, 3.0, 4 * GSL_DBL_EPSILON, "M_SQRT3^2");
543 gsl_test_rel (x, 3.1415926535897932384626433832795, 4 * GSL_DBL_EPSILON, "M_PI");
547 double x = 2 * M_PI_2;
548 gsl_test_rel (x, M_PI, 4 * GSL_DBL_EPSILON, "2*M_PI_2");
552 double x = 4 * M_PI_4;
553 gsl_test_rel (x, M_PI, 4 * GSL_DBL_EPSILON, "4*M_PI_4");
557 double x = pow(M_SQRTPI, 2.0);
558 gsl_test_rel (x, M_PI, 4 * GSL_DBL_EPSILON, "M_SQRTPI^2");
562 double x = pow(M_2_SQRTPI, 2.0);
563 gsl_test_rel (x, 4/M_PI, 4 * GSL_DBL_EPSILON, "M_SQRTPI^2");
568 gsl_test_rel (x, 1/M_PI, 4 * GSL_DBL_EPSILON, "M_1_SQRTPI");
573 gsl_test_rel (x, 2.0/M_PI, 4 * GSL_DBL_EPSILON, "M_2_PI");
577 double x = exp(M_LN10);
578 gsl_test_rel (x, 10, 4 * GSL_DBL_EPSILON, "exp(M_LN10)");
582 double x = exp(M_LN2);
583 gsl_test_rel (x, 2, 4 * GSL_DBL_EPSILON, "exp(M_LN2)");
587 double x = exp(M_LNPI);
588 gsl_test_rel (x, M_PI, 4 * GSL_DBL_EPSILON, "exp(M_LNPI)");
593 gsl_test_rel (x, 0.5772156649015328606065120900824, 4 * GSL_DBL_EPSILON, "M_EULER");
596 exit (gsl_test_summary ());