FORS Pipeline Reference Manual  5.0.9
fors_double.c
1 /* $Id: fors_double.c,v 1.5 2010-09-14 07:49:30 cizzo Exp $
2  *
3  * This file is part of the FORS Library
4  * Copyright (C) 2002-2010 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author: cizzo $
23  * $Date: 2010-09-14 07:49:30 $
24  * $Revision: 1.5 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include <fors_double.h>
33 
34 #include <fors_utils.h>
35 
36 #include <cpl.h>
37 #include <math.h>
38 #include <assert.h>
39 
40 #define LIST_DEFINE
41 #undef LIST_ELEM
42 #define LIST_ELEM double
43 #include <list.h>
44 
45 
53 /*
54  Copy constructor
55  */
56 double *
57 double_duplicate(const double *d)
58 {
59  double *dp = cpl_malloc(sizeof(*dp));
60  *dp = *d;
61  return dp;
62 }
63 
64 /*
65  @brief Destructor
66  @param d double
67  */
68 void
69 double_delete(double **d)
70 {
71  if (d && *d) {
72  cpl_free(*d); *d = NULL;
73  }
74 }
75 
82 double
83 double_eval(const double *d, void *data)
84 {
85  data = data;
86  return *d;
87 }
88 
89 #undef cleanup
90 #define cleanup
91 
100 double
101 double_subtract(double x, double dx,
102  double y, double dy,
103  double *error)
104 {
105  assure( error != NULL, return 0, NULL );
106 
107  assure( dx >= 0, return 0, NULL );
108  assure( dy >= 0, return 0, NULL );
109 
110  *error = sqrt( dx*dx + dy*dy );
111 
112  return x - y;
113 }
114 
115 
116 #undef cleanup
117 #define cleanup
118 
128 double
129 double_divide(double x, double dx,
130  double y, double dy,
131  double *error)
132 {
133  assure( error != NULL, return 0, NULL );
134 
135  assure( y*y > 0, return 0, NULL );
136  assure( dx >= 0, return 0, NULL );
137  assure( dy >= 0, return 0, NULL );
138 
139  *error = ( dx*dx + dy*dy * x*x / (y*y) ) / (y*y);
140  *error = sqrt(*error);
141 
142  return x/y;
143 }
144 
145 
146 #undef cleanup
147 #define cleanup
148 
159 double
160 double_atan2(double y, double dy,
161  double x, double dx,
162  double *error)
163 {
164  assure( error != NULL, return 0, NULL );
165  assure( dy >= 0, return 0, NULL );
166  assure( dx >= 0, return 0, NULL );
167  assure( (x*x + y*y)*(x*x + y*y) > 0, return 0, NULL ); /* angle undefined */
168 
169  /* using error propagation formula and d(atan(u))/du = 1/(1+u^2) */
170  *error = (dy*dy*x*x + dx*dx*y*y) / ((x*x + y*y)*(x*x + y*y));
171  *error = sqrt(*error);
172 
173  assert( *error >= 0 );
174 
175  return atan2(y, x);
176 }
#define assure(EXPR)
Definition: list.c:101