📄 test_sf.c
字号:
/* specfunc/test_sf.c * * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Gerard Jungman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* Author: G. Jungman */#include <config.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <gsl/gsl_math.h>#include <gsl/gsl_errno.h>#include <gsl/gsl_ieee_utils.h>#include <gsl/gsl_test.h>#include <gsl/gsl_sf.h>#include "test_sf.h"doubletest_sf_frac_diff(double x1, double x2){ if(x1 == 0.0 && x2 == 0.0) { return 0.0; } else if(x1 <= DBL_MAX && x2 <= DBL_MAX && (x1 + x2 != 0.0)) { return fabs((x1-x2)/(x1+x2)); } else { return 1.0; }}/* Check a result against a given value and tolerance. */inttest_sf_check_result(char * message_buff, gsl_sf_result r, double val, double tol){ int s = 0; double f = test_sf_frac_diff(val, r.val); if(fabs(val - r.val) > 2.0*r.err) s |= TEST_SF_INCONS; if(r.err < 0.0) s |= TEST_SF_ERRNEG; if(f > tol) s |= TEST_SF_TOLBAD; if(s != 0) { char buff[2048]; sprintf(buff, " expected: %20.16g\n", val); strcat(message_buff, buff); sprintf(buff, " obtained: %20.16g %20.16g %g\n", r.val, r.err, r.err/(fabs(r.val) + r.err)); strcat(message_buff, buff); sprintf(buff, " fracdiff: %20.16g\n", f); strcat(message_buff, buff); } if(s & TEST_SF_INCONS) { strcat(message_buff, " value/expected not consistent within reported error\n"); } if(s & TEST_SF_ERRNEG) { strcat(message_buff, " reported error negative\n"); } if(s & TEST_SF_TOLBAD) { strcat(message_buff, " value not within tolerance of expected value\n"); } return s;}/* Relax the condition on the agreement and on the usefulness * of the error estimate. */inttest_sf_check_result_relax(char * message_buff, gsl_sf_result r, double val, double tol){ int s = 0; double f = test_sf_frac_diff(val, r.val); if(f > GSL_MAX_DBL(TEST_SNGL,tol)) s |= TEST_SF_INCONS; if(r.err < 0.0) s |= TEST_SF_ERRNEG; if(f > tol) s |= TEST_SF_TOLBAD; if(s != 0) { char buff[2048]; sprintf(buff, " expected: %20.16g\n", val); strcat(message_buff, buff); sprintf(buff, " obtained: %20.16g %20.16g %g\n", r.val, r.err, r.err/(fabs(r.val) + r.err)); strcat(message_buff, buff); sprintf(buff, " fracdiff: %20.16g\n", f); strcat(message_buff, buff); } if(s & TEST_SF_INCONS) { strcat(message_buff, " value/expected not consistent MAX(tol,SINGLE_PREC)\n"); } if(s & TEST_SF_ERRNEG) { strcat(message_buff, " reported error negative\n"); } if(s & TEST_SF_TOLBAD) { strcat(message_buff, " value not within tolerance of expected value\n"); } return s;}/* Check a return value. */inttest_sf_check_return(char * message_buff, int val_return, int expected_return){ if(val_return != expected_return) { char buff[256]; sprintf(buff, " unexpected return code: %d\n", val_return); strcat(message_buff, buff); return TEST_SF_RETBAD; } else { return 0; }}inttest_sf (gsl_sf_result r, double val_in, double tol, int status, int expect_return, const char * desc){ char message_buff[4096]; int local_s = 0; message_buff[0] = '\0'; local_s |= test_sf_check_result(message_buff, r, val_in, tol); local_s |= test_sf_check_return(message_buff, status, expect_return); gsl_test(local_s, desc); if(local_s != 0) { /* printf(" %s %d\n", __FILE__, __LINE__); */ printf("%s", message_buff); printf(" %22.18g %22.18g\n", r.val, r.err); } return local_s;}inttest_sf_rlx (gsl_sf_result r, double val_in, double tol, int status, int expect_return, const char * desc){ char message_buff[4096]; int local_s = 0; message_buff[0] = '\0'; local_s |= test_sf_check_result_relax(message_buff, r, val_in, tol); local_s |= test_sf_check_return(message_buff, status, expect_return); gsl_test(local_s, desc); if(local_s != 0) { /* printf(" %s %d\n", __FILE__, __LINE__); */ printf("%s", message_buff); printf(" %22.18g %22.18g\n", r.val, r.err); } return local_s;}inttest_sf_2 (gsl_sf_result r1, double val1, double tol1, gsl_sf_result r2, double val2, double tol2, int status, int expect_return, const char * desc){ char message_buff[4096]; int local_s = 0; message_buff[0] = '\0'; local_s |= test_sf_check_result(message_buff, r1, val1, tol1); local_s |= test_sf_check_result(message_buff, r2, val2, tol2); local_s |= test_sf_check_return(message_buff, status, expect_return); gsl_test(local_s, desc); if(local_s != 0) { /* printf(" %s %d\n", __FILE__, __LINE__); */ printf("%s", message_buff); printf(" %22.18g %22.18g\n", r1.val, r1.err); printf(" %22.18g %22.18g\n", r2.val, r2.err); } return local_s;}inttest_sf_sgn (gsl_sf_result r, double sgn, double val_in, double tol, double expect_sgn, int status, int expect_return, const char * desc){ char message_buff[4096]; gsl_sf_result local_r; int local_s = 0; message_buff[0] = '\0'; local_r.val = sgn; local_r.err = 0.0; local_s |= test_sf_check_result(message_buff, r, val_in, tol); local_s |= test_sf_check_result(message_buff, local_r, expect_sgn, 0.0); local_s |= test_sf_check_return(message_buff, status, expect_return); gsl_test(local_s, desc); if(local_s != 0) { /* printf(" %s %d\n", __FILE__, __LINE__); */ printf("%s", message_buff); printf(" %22.18g %22.18g\n", r.val, r.err); } return local_s;}int test_clausen(void){ gsl_sf_result r; int s = 0; TEST_SF(s, gsl_sf_clausen_e, (M_PI/20.0, &r), 0.4478882448133546, TEST_TOL0, GSL_SUCCESS); TEST_SF(s, gsl_sf_clausen_e, (M_PI/6.0, &r), 0.8643791310538927, TEST_TOL0, GSL_SUCCESS); TEST_SF(s, gsl_sf_clausen_e, (M_PI/3.0, &r), 1.0149416064096535, TEST_TOL0, GSL_SUCCESS); TEST_SF(s, gsl_sf_clausen_e, ( 2.0*M_PI + M_PI/3.0, &r), 1.0149416064096535, TEST_TOL0, GSL_SUCCESS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -