30 |
* |
* |
31 |
*/ |
*/ |
32 |
|
|
33 |
/* |
/** @file |
34 |
|
* Safe function evaluator. |
35 |
|
* <pre> |
36 |
* Contents: Safe calculation module |
* Contents: Safe calculation module |
37 |
* |
* |
38 |
* Authors: Karl Westerberg, Joseph Zaher |
* Authors: Karl Westerberg, Joseph Zaher |
58 |
* version of errno. |
* version of errno. |
59 |
* At present, not all functions here are completely safe. |
* At present, not all functions here are completely safe. |
60 |
* It's floating point. Can they ever be? |
* It's floating point. Can they ever be? |
61 |
*/ |
* |
|
|
|
|
/* |
|
62 |
* When #including safe.h, make sure these files are #included first: |
* When #including safe.h, make sure these files are #included first: |
63 |
|
* #include "utilities/ascConfig.h" |
64 |
* #include "compiler.h" |
* #include "compiler.h" |
65 |
* #include "functype.h" |
* #include "functype.h" |
66 |
|
* </pre> |
67 |
|
* @todo Complete documentation of compiler/safe.h. |
68 |
*/ |
*/ |
69 |
|
|
|
|
|
70 |
#ifndef safe__already_included |
#ifndef safe__already_included |
71 |
#define safe__already_included |
#define safe__already_included |
72 |
|
|
|
|
|
73 |
/*extern int safe_print_errors;*/ |
/*extern int safe_print_errors;*/ |
74 |
|
|
75 |
extern void safe_error_to_stderr(enum safe_err *); |
extern void safe_error_to_stderr(enum safe_err *err); |
76 |
|
/**< |
|
/* |
|
77 |
* The variable safe_err will be set to the appropriate value whenever |
* The variable safe_err will be set to the appropriate value whenever |
78 |
* an undefined numerical calculation is attempted. |
* an undefined numerical calculation is attempted. |
79 |
* In this case, if the variable safe_print_errors is set to TRUE, |
* In this case, if the variable safe_print_errors is set to TRUE, |
84 |
* (it's our on silly version of errno...) |
* (it's our on silly version of errno...) |
85 |
*/ |
*/ |
86 |
|
|
87 |
#define safe_ERF_COEF 1.1283791670955130 /* = 2 / sqrt(PI) */ |
#define safe_ERF_COEF 1.1283791670955130 /**< = 2 / sqrt(PI) */ |
88 |
#define safe_LOG10_COEF 0.4342944819032518 /* = log10(e) = 1/ln(10) */ |
#define safe_LOG10_COEF 0.4342944819032518 /**< = log10(e) = 1/ln(10) */ |
89 |
#define safe_PI 3.1415926535897930 |
#define safe_PI 3.1415926535897930 /**< pi */ |
90 |
|
|
91 |
extern double safe_upower(double, unsigned, enum safe_err *); /* buggy */ |
extern double safe_upower(double x, unsigned y, enum safe_err *err); /**< buggy */ |
92 |
extern double safe_factorial(unsigned,enum safe_err *); |
/**< Compute x^n. Not Verified Code! */ |
93 |
extern double safe_rec(double,enum safe_err *); |
extern double safe_factorial(unsigned x, enum safe_err *err); |
94 |
extern double safe_cube(register double,enum safe_err *); |
/**< Compute n!. */ |
95 |
extern double safe_Dn_rec(double,int,enum safe_err *); |
extern double safe_rec(double x, enum safe_err *err); |
96 |
|
/**<Compute 1/x. */ |
97 |
|
extern double safe_cube(register double x, enum safe_err *err); |
98 |
|
/**<Compute x^3 with range check. */ |
99 |
|
extern double safe_Dn_rec(double x, int y, enum safe_err *err); |
100 |
|
/**< Compute n-th derivative of 1/x. */ |
101 |
#ifdef HAVE_ERF |
#ifdef HAVE_ERF |
102 |
extern double safe_erf_inv(double,enum safe_err *); |
extern double safe_erf_inv(double x, enum safe_err *err); |
103 |
|
/**< Compute inverse of erf. */ |
104 |
#endif /* HAVE_ERF */ |
#endif /* HAVE_ERF */ |
105 |
extern double safe_lnm_inv(double,enum safe_err *); |
extern double safe_lnm_inv(double x, enum safe_err *err); |
106 |
extern int safe_is_int(double,enum safe_err *); |
extern int safe_is_int(double x, enum safe_err *err); |
107 |
/* |
/**< 0,1 ==> even/odd int,else ==> not an int. */ |
|
* value = safe_upower(x,n,safe) Compute x^n. Not Verified Code! |
|
|
* value = safe_factorial(n,safe) Compute n!. |
|
|
* value = safe_rec(x,safe) Compute 1/x. |
|
|
* value = safe_cube(x,safe) Compute x^3 with range check |
|
|
* value = safe_Dn_rec(x,n,safe) Compute n-th derivative of 1/x. |
|
|
* value = safe_erf_inv(x,safe) Compute inverse of erf. |
|
|
* isint = safe_is_int(x,safe) 0,1 ==> even/odd int,else ==> not an int. |
|
|
* double value,x; |
|
|
* unsigned n; |
|
|
* enum safe_err *safe |
|
|
* int isint; |
|
|
*/ |
|
108 |
|
|
109 |
/* All of the following safe_XXXX_Di must resolve to C functions |
/* All of the following safe_XXXX_Di must resolve to C functions |
110 |
* rather than macros because safe_func_Di takes the address of them. |
* rather than macros because safe_func_Di takes the address of them. |
111 |
*/ |
*/ |
112 |
|
|
113 |
extern double safe_sin_D0(double,enum safe_err *); |
extern double safe_sin_D0(double x, enum safe_err *safe); |
114 |
extern double safe_sinh_D0(double,enum safe_err *); |
extern double safe_sinh_D0(double x, enum safe_err *safe); |
115 |
extern double safe_cos_D0(double,enum safe_err *); |
extern double safe_cos_D0(double x, enum safe_err *safe); |
116 |
extern double safe_cosh_D0(double,enum safe_err *); |
extern double safe_cosh_D0(double x, enum safe_err *safe); |
117 |
extern double safe_tan_D0(double,enum safe_err *); |
extern double safe_tan_D0(double x, enum safe_err *safe); |
118 |
extern double safe_tanh_D0(double,enum safe_err *); |
extern double safe_tanh_D0(double x, enum safe_err *safe); |
119 |
extern double safe_arctan_D0(double,enum safe_err *); |
extern double safe_arctan_D0(double x, enum safe_err *safe); |
120 |
extern double safe_arcsin_D0(double,enum safe_err *); |
extern double safe_arcsin_D0(double x, enum safe_err *safe); |
121 |
extern double safe_arcsinh_D0(double,enum safe_err *); |
extern double safe_arcsinh_D0(double x, enum safe_err *safe); |
122 |
extern double safe_arccos_D0(double,enum safe_err *); |
extern double safe_arccos_D0(double x, enum safe_err *safe); |
123 |
extern double safe_arccosh_D0(double,enum safe_err *); |
extern double safe_arccosh_D0(double x, enum safe_err *safe); |
124 |
extern double safe_arctanh_D0(double,enum safe_err *); |
extern double safe_arctanh_D0(double x, enum safe_err *safe); |
125 |
#ifdef HAVE_ERF |
#ifdef HAVE_ERF |
126 |
extern double safe_erf_D0(double,enum safe_err *); |
extern double safe_erf_D0(double x, enum safe_err *safe); |
127 |
#endif /* HAVE_ERF */ |
#endif /* HAVE_ERF */ |
128 |
extern double safe_exp_D0(double,enum safe_err *); |
extern double safe_exp_D0(double x, enum safe_err *safe); |
129 |
extern double safe_ln_D0(double,enum safe_err *); |
extern double safe_ln_D0(double x, enum safe_err *safe); |
130 |
extern double safe_lnm_D0(double,enum safe_err *); |
extern double safe_lnm_D0(double x, enum safe_err *safe); |
131 |
extern double safe_log_D0(double,enum safe_err *); |
extern double safe_log_D0(double x, enum safe_err *safe); |
132 |
extern double safe_sqr_D0(double,enum safe_err *); |
extern double safe_sqr_D0(double x, enum safe_err *safe); |
133 |
extern double safe_sqrt_D0(double,enum safe_err *); |
extern double safe_sqrt_D0(double x, enum safe_err *safe); |
134 |
extern double safe_cbrt_D0(double,enum safe_err *); |
extern double safe_cbrt_D0(double x, enum safe_err *safe); |
135 |
extern double safe_fabs_D0(double,enum safe_err *); |
extern double safe_fabs_D0(double x, enum safe_err *safe); |
136 |
extern double safe_hold_D0(double,enum safe_err *); |
extern double safe_hold_D0(double x, enum safe_err *safe); |
137 |
/* |
/* |
138 |
* value = safe_<uop>_D0(x) |
* value = safe_<uop>_D0(x) |
139 |
* double value,x; |
* double value,x; |
142 |
* Computes value of unary operator. |
* Computes value of unary operator. |
143 |
*/ |
*/ |
144 |
|
|
145 |
extern double safe_sin_D1(double,enum safe_err *); |
extern double safe_sin_D1(double x, enum safe_err *safe); |
146 |
extern double safe_sinh_D1(double,enum safe_err *); |
extern double safe_sinh_D1(double x, enum safe_err *safe); |
147 |
extern double safe_cos_D1(double,enum safe_err *); |
extern double safe_cos_D1(double x, enum safe_err *safe); |
148 |
extern double safe_cosh_D1(double,enum safe_err *); |
extern double safe_cosh_D1(double x, enum safe_err *safe); |
149 |
extern double safe_tan_D1(double,enum safe_err *); |
extern double safe_tan_D1(double x, enum safe_err *safe); |
150 |
extern double safe_tanh_D1(double,enum safe_err *); |
extern double safe_tanh_D1(double x, enum safe_err *safe); |
151 |
extern double safe_arcsin_D1(double,enum safe_err *); |
extern double safe_arcsin_D1(double x, enum safe_err *safe); |
152 |
extern double safe_arcsinh_D1(double,enum safe_err *); |
extern double safe_arcsinh_D1(double x, enum safe_err *safe); |
153 |
extern double safe_arccos_D1(double,enum safe_err *); |
extern double safe_arccos_D1(double x, enum safe_err *safe); |
154 |
extern double safe_arccosh_D1(double,enum safe_err *); |
extern double safe_arccosh_D1(double x, enum safe_err *safe); |
155 |
extern double safe_arctan_D1(double,enum safe_err *); |
extern double safe_arctan_D1(double x, enum safe_err *safe); |
156 |
extern double safe_arctanh_D1(double,enum safe_err *); |
extern double safe_arctanh_D1(double x, enum safe_err *safe); |
157 |
#ifdef HAVE_ERF |
#ifdef HAVE_ERF |
158 |
extern double safe_erf_D1(double,enum safe_err *); |
extern double safe_erf_D1(double x, enum safe_err *safe); |
159 |
#endif /* HAVE_ERF */ |
#endif /* HAVE_ERF */ |
160 |
extern double safe_exp_D1(double,enum safe_err *); |
extern double safe_exp_D1(double x, enum safe_err *safe); |
161 |
extern double safe_ln_D1(double,enum safe_err *); |
extern double safe_ln_D1(double x, enum safe_err *safe); |
162 |
extern double safe_lnm_D1(double,enum safe_err *); |
extern double safe_lnm_D1(double x, enum safe_err *safe); |
163 |
extern double safe_log_D1(double,enum safe_err *); |
extern double safe_log_D1(double x, enum safe_err *safe); |
164 |
extern double safe_sqr_D1(double,enum safe_err *); |
extern double safe_sqr_D1(double x, enum safe_err *safe); |
165 |
extern double safe_sqrt_D1(double,enum safe_err *); |
extern double safe_sqrt_D1(double x, enum safe_err *safe); |
166 |
extern double safe_cube_D0(double,enum safe_err *); |
extern double safe_cube_D0(double x, enum safe_err *safe); |
167 |
extern double safe_cube_D1(double,enum safe_err *); |
extern double safe_cube_D1(double x, enum safe_err *safe); |
168 |
extern double safe_cbrt_D1(double,enum safe_err *); |
extern double safe_cbrt_D1(double x, enum safe_err *safe); |
169 |
extern double safe_fabs_D1(double,enum safe_err *); |
extern double safe_fabs_D1(double x, enum safe_err *safe); |
170 |
extern double safe_hold_D1(double,enum safe_err *); |
extern double safe_hold_D1(double x, enum safe_err *safe); |
171 |
/* |
/* |
172 |
* value = safe_<uop>_D1(x) |
* value = safe_<uop>_D1(x) |
173 |
* double value,x; |
* double value,x; |
176 |
* Computes first derivative of unary operator. |
* Computes first derivative of unary operator. |
177 |
*/ |
*/ |
178 |
|
|
179 |
|
extern double safe_sin_D2(double x, enum safe_err *safe); |
180 |
extern double safe_sin_D2(double,enum safe_err *); |
extern double safe_sinh_D2(double x, enum safe_err *safe); |
181 |
extern double safe_sinh_D2(double,enum safe_err *); |
extern double safe_cos_D2(double x, enum safe_err *safe); |
182 |
extern double safe_cos_D2(double,enum safe_err *); |
extern double safe_cosh_D2(double x, enum safe_err *safe); |
183 |
extern double safe_cosh_D2(double,enum safe_err *); |
extern double safe_tan_D2(double x, enum safe_err *safe); |
184 |
extern double safe_tan_D2(double,enum safe_err *); |
extern double safe_tanh_D2(double x, enum safe_err *safe); |
185 |
extern double safe_tanh_D2(double,enum safe_err *); |
extern double safe_arcsin_D2(double x, enum safe_err *safe); |
186 |
extern double safe_arcsin_D2(double,enum safe_err *); |
extern double safe_arcsinh_D2(double x, enum safe_err *safe); |
187 |
extern double safe_arcsinh_D2(double,enum safe_err *); |
extern double safe_arccos_D2(double x, enum safe_err *safe); |
188 |
extern double safe_arccos_D2(double,enum safe_err *); |
extern double safe_arccosh_D2(double x, enum safe_err *safe); |
189 |
extern double safe_arccosh_D2(double,enum safe_err *); |
extern double safe_arctan_D2(double x, enum safe_err *safe); |
190 |
extern double safe_arctan_D2(double,enum safe_err *); |
extern double safe_arctanh_D2(double x, enum safe_err *safe); |
|
extern double safe_arctanh_D2(double,enum safe_err *); |
|
191 |
#ifdef HAVE_ERF |
#ifdef HAVE_ERF |
192 |
extern double safe_erf_D2(double,enum safe_err *); |
extern double safe_erf_D2(double x, enum safe_err *safe); |
193 |
#endif /* HAVE_ERF */ |
#endif /* HAVE_ERF */ |
194 |
extern double safe_exp_D2(double,enum safe_err *); |
extern double safe_exp_D2(double x, enum safe_err *safe); |
195 |
extern double safe_ln_D2(double,enum safe_err *); |
extern double safe_ln_D2(double x, enum safe_err *safe); |
196 |
extern double safe_lnm_D2(double,enum safe_err *); |
extern double safe_lnm_D2(double x, enum safe_err *safe); |
197 |
extern double safe_log_D2(double,enum safe_err *); |
extern double safe_log_D2(double x, enum safe_err *safe); |
198 |
extern double safe_sqr_D2(double,enum safe_err *); |
extern double safe_sqr_D2(double x, enum safe_err *safe); |
199 |
extern double safe_sqrt_D2(double,enum safe_err *); |
extern double safe_sqrt_D2(double x, enum safe_err *safe); |
200 |
extern double safe_cube_D2(double,enum safe_err *); |
extern double safe_cube_D2(double x, enum safe_err *safe); |
201 |
extern double safe_cbrt_D2(double,enum safe_err *); |
extern double safe_cbrt_D2(double x, enum safe_err *safe); |
202 |
extern double safe_fabs_D2(double,enum safe_err *); |
extern double safe_fabs_D2(double x, enum safe_err *safe); |
203 |
#define safe_hold_D2 safe_hold_D1 |
#define safe_hold_D2 safe_hold_D1 |
204 |
/* |
/* |
205 |
* value = safe_<uop>_D2(x) |
* value = safe_<uop>_D2(x) |
209 |
* Computes second derivative of unary operator. |
* Computes second derivative of unary operator. |
210 |
*/ |
*/ |
211 |
|
|
212 |
extern double safe_arcsin_Dn(double,int,enum safe_err *); |
extern double safe_arcsin_Dn(double x, int n, enum safe_err *safe); |
213 |
extern double safe_arccos_Dn(double,int,enum safe_err *); |
extern double safe_arccos_Dn(double x, int n, enum safe_err *safe); |
214 |
extern double safe_arctan_Dn(double,int,enum safe_err *); |
extern double safe_arctan_Dn(double x, int n, enum safe_err *safe); |
215 |
extern double safe_cos_Dn(double,int,enum safe_err *); |
extern double safe_cos_Dn(double x, int n, enum safe_err *safe); |
216 |
extern double safe_sin_Dn(double,int,enum safe_err *); |
extern double safe_sin_Dn(double x, int n, enum safe_err *safe); |
217 |
#ifdef HAVE_ERF |
#ifdef HAVE_ERF |
218 |
extern double safe_erf_Dn(double,int,enum safe_err *); |
extern double safe_erf_Dn(double x, int n, enum safe_err *safe); |
219 |
#endif /* HAVE_ERF */ |
#endif /* HAVE_ERF */ |
220 |
extern double safe_exp_Dn(double,int,enum safe_err *); |
extern double safe_exp_Dn(double x, int n, enum safe_err *safe); |
221 |
extern double safe_ln_Dn(double,int,enum safe_err *); |
extern double safe_ln_Dn(double x, int n, enum safe_err *safe); |
222 |
extern double safe_log_Dn(double,int,enum safe_err *); |
extern double safe_log_Dn(double x, int n, enum safe_err *safe); |
223 |
extern double safe_sqr_Dn(double,int,enum safe_err *); |
extern double safe_sqr_Dn(double x, int n, enum safe_err *safe); |
224 |
extern double safe_sqrt_Dn(double,int,enum safe_err *); |
extern double safe_sqrt_Dn(double x, int n, enum safe_err *safe); |
225 |
extern double safe_tan_Dn(double,int,enum safe_err *); |
extern double safe_tan_Dn(double x, int n, enum safe_err *safe); |
226 |
extern double safe_fabs_Dn(double,int,enum safe_err *); |
extern double safe_fabs_Dn(double x, int n, enum safe_err *safe); |
227 |
#define safe_hold_Dn safe_hold_D1 |
#define safe_hold_Dn safe_hold_D1 |
228 |
/* |
/* |
229 |
* value = safe_<uop>_Dn(x,n) |
* value = safe_<uop>_Dn(x,n) |
239 |
#define safe_mul_D0(x,y,safe) ((x)*(y)) |
#define safe_mul_D0(x,y,safe) ((x)*(y)) |
240 |
#define safe_div_D0(x,y,safe) safe_mul_D0((x),safe_rec(y,safe),(safe)) |
#define safe_div_D0(x,y,safe) safe_mul_D0((x),safe_rec(y,safe),(safe)) |
241 |
#define safe_ipow_D0(x,y,safe) asc_ipow((x),ascnint(y)) |
#define safe_ipow_D0(x,y,safe) asc_ipow((x),ascnint(y)) |
242 |
extern double safe_pow_D0(double,double,enum safe_err *); |
extern double safe_pow_D0(double x, double y, enum safe_err *safe); |
243 |
/* |
/**< |
244 |
* value = safe_<binop>_D0(x,y,safe) |
* <!-- value = safe_<binop>_D0(x,y,safe) --> |
245 |
* double value,x,y; |
* <!-- double value,x,y; --> |
246 |
* enum safe_err *safe |
* <!-- enum safe_err *safe --> |
247 |
* |
* |
248 |
* Computes x <binop> y and returns the value. |
* Computes x <binop> y and returns the value. |
249 |
*/ |
*/ |
251 |
#define safe_add_D1(x,y,wrt,safe) (1.0) |
#define safe_add_D1(x,y,wrt,safe) (1.0) |
252 |
#define safe_sub_D1(x,y,wrt,safe) (1.0 - 2.0*(wrt)) |
#define safe_sub_D1(x,y,wrt,safe) (1.0 - 2.0*(wrt)) |
253 |
#define safe_mul_D1(x,y,wrt,safe) ((wrt)==0 ? (y) : (x)) |
#define safe_mul_D1(x,y,wrt,safe) ((wrt)==0 ? (y) : (x)) |
254 |
extern double safe_div_D1(double,double,int,enum safe_err *); |
extern double safe_div_D1(double x, double y, int wrt, enum safe_err *safe); |
255 |
extern double safe_ipow_D1(double,double,int,enum safe_err *); |
extern double safe_ipow_D1(double x, double y, int wrt, enum safe_err *safe); |
256 |
extern double safe_pow_D1(double,double,int,enum safe_err *); |
extern double safe_pow_D1(double x, double y, int wrt, enum safe_err *safe); |
257 |
/* |
/* |
258 |
* diff = safe_<binop>_D1(x,y,wrt) |
* diff = safe_<binop>_D1(x,y,wrt) |
259 |
* double diff,x,y; |
* double diff,x,y; |
266 |
#define safe_add_D2(x,y,wrt1,wrt2,safe) (0.0) |
#define safe_add_D2(x,y,wrt1,wrt2,safe) (0.0) |
267 |
#define safe_sub_D2(x,y,wrt1,wrt2,safe) (0.0) |
#define safe_sub_D2(x,y,wrt1,wrt2,safe) (0.0) |
268 |
#define safe_mul_D2(x,y,wrt1,wrt2,safe) ((wrt1)!=(wrt2)?1.0:0.0) |
#define safe_mul_D2(x,y,wrt1,wrt2,safe) ((wrt1)!=(wrt2)?1.0:0.0) |
269 |
extern double safe_div_D2(double,double,int,int,enum safe_err *); |
extern double safe_div_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
270 |
extern double safe_ipow_D2(double, double, int, int,enum safe_err *); |
extern double safe_ipow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
271 |
extern double safe_pow_D2(double, double, int, int,enum safe_err *); |
extern double safe_pow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
272 |
/* |
/* |
273 |
* diff2 = safe_<binop>_D2(x,y,wrt1,wrt2,safe) |
* diff2 = safe_<binop>_D2(x,y,wrt1,wrt2,safe) |
274 |
* double diff2,x,y; |
* double diff2,x,y; |
285 |
* wrt1 = 1 wrt2 = 1 ==> d^2f/dy^2 |
* wrt1 = 1 wrt2 = 1 ==> d^2f/dy^2 |
286 |
*/ |
*/ |
287 |
|
|
288 |
extern double safe_add_Dn(double,double,int,int,enum safe_err *); |
extern double safe_add_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
289 |
extern double safe_sub_Dn(double,double,int,int,enum safe_err *); |
extern double safe_sub_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
290 |
extern double safe_mul_Dn(double,double,int,int,enum safe_err *); |
extern double safe_mul_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
291 |
extern double safe_div_Dn(double,double,int,int,enum safe_err *); |
extern double safe_div_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
292 |
extern double |
extern double |
293 |
safe_pow_Dn(double,double,int,int,enum safe_err *); /*this one not verified!*/ |
safe_pow_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
294 |
|
/**< @todo this one not verified!*/ |
295 |
/* |
/* |
296 |
* diffn = safe_<binop>_Dn(x,y,nwrt0,nwrt1) |
* diffn = safe_<binop>_Dn(x,y,nwrt0,nwrt1) |
297 |
* double diffn,x,y; |
* double diffn,x,y; |
301 |
* of x <binop> y. |
* of x <binop> y. |
302 |
*/ |
*/ |
303 |
|
|
304 |
#endif |
#endif /* safe__already_included */ |
305 |
|
|