/src/r-source/src/nmath/pbinom.c
Line | Count | Source |
1 | | /* |
2 | | * Mathlib : A C Library of Special Functions |
3 | | * Copyright (C) 1998 Ross Ihaka |
4 | | * Copyright (C) 2000-2015 The R Core Team |
5 | | * Copyright (C) 2004-2015 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 binomial distribution. |
24 | | */ |
25 | | #include "nmath.h" |
26 | | #include "dpq.h" |
27 | | |
28 | | double pbinom(double x, double n, double p, int lower_tail, int log_p) |
29 | 0 | { |
30 | 0 | #ifdef IEEE_754 |
31 | 0 | if (ISNAN(x) || ISNAN(n) || ISNAN(p)) |
32 | 0 | return x + n + p; |
33 | 0 | if (!R_FINITE(n) || !R_FINITE(p)) ML_WARN_return_NAN; |
34 | |
|
35 | 0 | #endif |
36 | 0 | if(R_nonint(n)) { |
37 | 0 | MATHLIB_WARNING(_("non-integer n = %f"), n); |
38 | 0 | ML_WARN_return_NAN; |
39 | 0 | } |
40 | 0 | n = R_forceint(n); |
41 | | /* PR#8560: n=0 is a valid value */ |
42 | 0 | if(n < 0 || p < 0 || p > 1) ML_WARN_return_NAN; |
43 | |
|
44 | 0 | if (x < 0) return R_DT_0; |
45 | 0 | x = floor(x + 1e-7); |
46 | 0 | if (n <= x) return R_DT_1; |
47 | 0 | return pbeta(p, x + 1, n - x, !lower_tail, log_p); |
48 | 0 | } |