The R Project SVN R

Rev

Rev 11723 | Rev 36986 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11723 Rev 30227
Line 38... Line 38...
38
 
38
 
39
#include "nmath.h"
39
#include "nmath.h"
40
#include "dpq.h"
40
#include "dpq.h"
41
 
41
 
42
double dbinom_raw(double x, double n, double p, double q, int give_log)
42
double dbinom_raw(double x, double n, double p, double q, int give_log)
43
{ 
43
{
44
    double f, lc;
44
    double f, lc;
45
 
45
 
46
    if (p == 0) return((x == 0) ? R_D__1 : R_D__0);
46
    if (p == 0) return((x == 0) ? R_D__1 : R_D__0);
47
    if (q == 0) return((x == n) ? R_D__1 : R_D__0);
47
    if (q == 0) return((x == n) ? R_D__1 : R_D__0);
48
 
48
 
49
    if (x == 0) { 
49
    if (x == 0) {
50
	if(n == 0) return R_D__1;
50
	if(n == 0) return R_D__1;
51
	lc = (p < 0.1) ? -bd0(n,n*q) - n*p : n*log(q);
51
	lc = (p < 0.1) ? -bd0(n,n*q) - n*p : n*log(q);
52
	return( R_D_exp(lc) );
52
	return( R_D_exp(lc) );
53
    }
53
    }
54
    if (x == n) { 
54
    if (x == n) {
55
	lc = (q < 0.1) ? -bd0(n,n*p) - n*q : n*log(p);
55
	lc = (q < 0.1) ? -bd0(n,n*p) - n*q : n*log(p);
56
	return( R_D_exp(lc) );
56
	return( R_D_exp(lc) );
57
    }
57
    }
58
    if (x < 0 || x > n) return( R_D__0 );
58
    if (x < 0 || x > n) return( R_D__0 );
59
 
59
 
Line 62... Line 62...
62
 
62
 
63
    return R_D_fexp(f,lc);
63
    return R_D_fexp(f,lc);
64
}
64
}
65
 
65
 
66
double dbinom(double x, double n, double p, int give_log)
66
double dbinom(double x, double n, double p, int give_log)
67
{ 
67
{
68
#ifdef IEEE_754
68
#ifdef IEEE_754
69
    /* NaNs propagated correctly */
69
    /* NaNs propagated correctly */
70
    if (ISNAN(x) || ISNAN(n) || ISNAN(p)) return x + n + p;
70
    if (ISNAN(x) || ISNAN(n) || ISNAN(p)) return x + n + p;
71
#endif
71
#endif
72
 
72
 
73
  if (p < 0 || p > 1 || R_D_notnnegint(n)) ML_ERR_return_NAN;
73
    if (p < 0 || p > 1 || R_D_negInonint(n))
74
  R_D_nonint_check(x);
-
 
75
  
-
 
76
  n = R_D_forceint(n);
74
	ML_ERR_return_NAN;
77
  x = R_D_forceint(x);
75
    R_D_nonint_check(x);
78
 
76
 
-
 
77
    n = R_D_forceint(n);
-
 
78
    x = R_D_forceint(x);
-
 
79
 
79
  return dbinom_raw(x,n,p,1-p,give_log);
80
    return dbinom_raw(x,n,p,1-p,give_log);
80
}
81
}