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/dgeom.c
Line
Count
Source
1
/*
2
 *  AUTHOR
3
 *    Catherine Loader, catherine@research.bell-labs.com.
4
 *    October 23, 2000.
5
 *
6
 *  Merge in to R:
7
 *  Copyright (C) 2000-2014 The R Core Team
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, a copy is available at
21
 *  https://www.R-project.org/Licenses/
22
 *
23
 *
24
 *  DESCRIPTION
25
 *
26
 *    Computes the geometric probabilities, Pr(X=x) = p(1-p)^x.
27
 */
28
29
#include "nmath.h"
30
#include "dpq.h"
31
32
double dgeom(double x, double p, int give_log)
33
0
{
34
0
#ifdef IEEE_754
35
0
    if (ISNAN(x) || ISNAN(p)) return x + p;
36
0
#endif
37
38
0
    if (p <= 0 || p > 1) ML_WARN_return_NAN;
39
40
0
    R_D_nonint_check(x);
41
0
    if (x < 0 || !R_FINITE(x) || p == 0) return R_D__0;
42
0
    x = R_forceint(x);
43
44
    /* prob = (1-p)^x, stable for small p */
45
0
    double prob = dbinom_raw(0.,x, p,1-p, give_log);
46
47
0
    return((give_log) ? log(p) + prob : p*prob);
48
0
}