1 /*
2 * Mathlib : A C Library of Special Functions
3 * Copyright (C) 1998 Ross Ihaka
4 * Copyright (C) 2000-2014 The R Core Team
5 * Copyright (C) 2004 The R Foundation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, a copy is available at
19 * https://www.R-project.org/Licenses/
20 *
21 * DESCRIPTION
22 *
23 * The distribution function of the Cauchy distribution.
24 */
25
26 #include <config.h>
27
28 #ifdef HAVE_ATANPI
29 double atanpi(double);
30 #endif
31
32 #include "nmath.h"
33 #include "dpq.h"
34
pcauchy(double x,double location,double scale,int lower_tail,int log_p)35 double pcauchy(double x, double location, double scale,
36 int lower_tail, int log_p)
37 {
38 #ifdef IEEE_754
39 if (ISNAN(x) || ISNAN(location) || ISNAN(scale))
40 return x + location + scale;
41 #endif
42 if (scale <= 0) ML_ERR_return_NAN;
43
44 x = (x - location) / scale;
45 if (ISNAN(x)) ML_ERR_return_NAN;
46 #ifdef IEEE_754
47 if(!R_FINITE(x)) {
48 if(x < 0) return R_DT_0;
49 else return R_DT_1;
50 }
51 #endif
52 if (!lower_tail)
53 x = -x;
54 /* for large x, the standard formula suffers from cancellation.
55 * This is from Morten Welinder thanks to Ian Smith's atan(1/x) : */
56 #ifdef HAVE_ATANPI
57 if (fabs(x) > 1) {
58 double y = atanpi(1/x);
59 return (x > 0) ? R_D_Clog(y) : R_D_val(-y);
60 } else
61 return R_D_val(0.5 + atanpi(x));
62 #else
63 if (fabs(x) > 1) {
64 double y = atan(1/x) / M_PI;
65 return (x > 0) ? R_D_Clog(y) : R_D_val(-y);
66 } else
67 return R_D_val(0.5 + atan(x) / M_PI);
68 #endif
69 }
70