File: cpl_math-test.c

package info (click to toggle)
cpl 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,764 kB
  • sloc: ansic: 111,368; sh: 14,549; makefile: 626
file content (134 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download
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
/* $Id: cpl_math-test.c,v 1.12 2010/11/11 09:23:18 llundin Exp $
 *
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2008 European Southern Observatory
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * $Author: llundin $
 * $Date: 2010/11/11 09:23:18 $
 * $Revision: 1.12 $
 * $Name: cpl-6_1_1 $
 */

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>
#include <float.h>

#include "cpl_math_const.h"
#include "cpl_test.h"

/*-----------------------------------------------------------------------------
                                  Defines
 -----------------------------------------------------------------------------*/

/* Required precision in multiples of the machine precision */
#ifndef CPL_MATH_PRECISION
#define CPL_MATH_PRECISION 1.0
#endif

/*-----------------------------------------------------------------------------
                                  Main
 -----------------------------------------------------------------------------*/
int main(void)
{
    const double prec = CPL_MATH_PRECISION * DBL_EPSILON;

    cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);

    /* Insert tests below */

    /* The constants are of type double */
    cpl_test_eq(sizeof(CPL_MATH_PI), sizeof(double));

    /* Expressions with just a subtraction must be accurate to 1 unit
       of machine precision, expressions also involving division must be
       accurate to 2 units of machine precision */

    /* pi */
    cpl_test_rel(atan2(0.0, -1.0), CPL_MATH_PI, prec);

    /* 2 pi */
    cpl_test_rel(2.0 * atan2(0.0, -1.0), CPL_MATH_2PI, prec);

    /* pi/2 */
    cpl_test_rel(atan2(1.0, 0.0), CPL_MATH_PI_2, prec);

    /* pi/4 */
    cpl_test_rel(atan2(1.0, 1.0), CPL_MATH_PI_4, prec);

    /* 1/pi */
    cpl_test_rel(1.0/atan2(0.0, -1.0), CPL_MATH_1_PI, 2.0 * prec);

    /* 2/pi */
    cpl_test_rel(1.0/atan2(1.0, 0.0), CPL_MATH_2_PI, 2.0 * prec);

    /* 4/pi */
    cpl_test_rel(1.0/atan2(1.0, 1.0), CPL_MATH_4_PI, 2.0 * prec);

    /* sqrt(2pi) */
    cpl_test_rel(sqrt(2.0 * atan2(0.0, -1.0)), CPL_MATH_SQRT2PI, 2.0 * prec);

    /* 2 / sqrt(pi) */
    cpl_test_rel(1.0 / sqrt(atan2(1.0, 1.0)), CPL_MATH_2_SQRTPI, 2.0 * prec);

    /* sqrt(2) */
    cpl_test_rel(sqrt(2.0), CPL_MATH_SQRT2, prec);

    /* sqrt(3) */
    cpl_test_rel(sqrt(3.0), CPL_MATH_SQRT3, prec);

    /* sqrt(1/2) */
    cpl_test_rel(sqrt(0.5), CPL_MATH_SQRT1_2, prec);

    /* exp(1) */
    cpl_test_rel(exp(1.0), CPL_MATH_E, prec);

    /* log(2) */
    cpl_test_rel(log(2.0), CPL_MATH_LN2, prec);

    /* log(10) */
    cpl_test_rel(log(10.0), CPL_MATH_LN10, prec);

    /* log2(e) */
    cpl_test_rel(1.0/log(2.0), CPL_MATH_LOG2E, 2.0 * prec);

    /* log10(e) */
    cpl_test_rel(log10(exp(1.0)), CPL_MATH_LOG10E, prec);

    /* 180/pi */
    cpl_test_rel(45.0 / atan2(1.0, 1.0), CPL_MATH_DEG_RAD, 2.0 * prec);

    /* pi/180 */
    cpl_test_rel(CPL_MATH_RAD_DEG * CPL_MATH_DEG_RAD, 1.0, prec);

    /* 2.0*sqrt(2.0*log(2.0)) */
    cpl_test_rel(2.0*sqrt(2.0*log(2.0)), CPL_MATH_FWHM_SIG, prec);

    /* 0.5/sqrt(2.0*log(2.0)) */
    cpl_test_rel(0.5/sqrt(2.0*log(2.0)), CPL_MATH_SIG_FWHM, 2.0 * prec);


    return cpl_test_end(0);
}