Coverage Report

Created: 2026-10-16 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/nmath/qbinom.c
Line
Count
Source
1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1999-2025 The R Core Team
4
 *  Copyright (C) 2003-2021 The R Foundation
5
 *  Copyright (C) 1998 Ross Ihaka
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 quantile function of the binomial distribution.
24
 *
25
 *  METHOD
26
 *
27
 *  Uses the Cornish-Fisher Expansion to include a skewness
28
 *  correction to a normal approximation.  This gives an
29
 *  initial value which never seems to be off by more than
30
 *  1 or 2.  A search is then conducted of values close to
31
 *  this initial start point.
32
 */
33
#include "nmath.h"
34
#include "dpq.h"
35
36
// notably used in  " ./qDiscrete_search.h ":
37
#ifdef DEBUG_qbinom
38
# define R_DBG_printf(...) REprintf(__VA_ARGS__)
39
#else
40
# define R_DBG_printf(...)
41
#endif
42
43
44
#define _thisDIST_ binom
45
#define _dist_PARS_DECL_ double n, double pr
46
#define _dist_PARS_      n, pr
47
0
#define _dist_MAX_y  n
48
//                  ===  Binomial  Y <= n
49
50
#include "qDiscrete_search.h"
51
//        ------------------>  do_search() and all called by q_DISCRETE_*() below
52
53
double qbinom(double p, double n, double pr, int lower_tail, int log_p)
54
0
{
55
0
#ifdef IEEE_754
56
0
    if (ISNAN(p) || ISNAN(n) || ISNAN(pr))
57
0
  return p + n + pr;
58
0
#endif
59
0
    if(!R_FINITE(n) || !R_FINITE(pr))
60
0
  ML_WARN_return_NAN;
61
    /* if log_p is true, p = -Inf is a legitimate value */
62
0
    if(!R_FINITE(p) && !log_p)
63
0
  ML_WARN_return_NAN;
64
65
0
    n = R_forceint(n);
66
67
0
    if (pr < 0 || pr > 1 || n < 0)
68
0
  ML_WARN_return_NAN;
69
70
0
    R_Q_P01_boundaries(p, 0, n);
71
72
0
    if (pr == 0. || n == 0) return 0.;
73
0
    if (pr == 1.)           return n; /* covers the full range of the distribution */
74
75
    // (NB: unavoidable cancellation for pr ~= 1)
76
0
    double
77
0
  q = 1 - pr,
78
0
  mu = n * pr,
79
0
  sigma = sqrt(n * pr * q),
80
0
  gamma = (q - pr) / sigma;
81
82
0
    R_DBG_printf("qbinom(p=%.12g, n=%.15g, pr=%.7g, l.t.=%d, log=%d): sigma=%g, gamma=%g;\n",
83
0
     p, n,pr, lower_tail, log_p, sigma, gamma);
84
85
0
    q_DISCRETE_01_CHECKS();
86
    q_DISCRETE_BODY();
87
0
}