The R Project SVN R

Rev

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

Rev 59896 Rev 59898
Line 38... Line 38...
38
 *    (n \\ k ) == (n \\ n-k)  is preserved for non-negative integer n.
38
 *    (n \\ k ) == (n \\ n-k)  is preserved for non-negative integer n.
39
 */
39
 */
40
 
40
 
41
#include "nmath.h"
41
#include "nmath.h"
42
 
42
 
-
 
43
/* These are recursive, so we should do a stack check */
-
 
44
 
-
 
45
#ifndef MATHLIB_STANDALONE
-
 
46
void R_CheckStack(void);
-
 
47
#endif
-
 
48
 
43
double attribute_hidden lfastchoose(double n, double k)
49
double attribute_hidden lfastchoose(double n, double k)
44
{
50
{
45
    return -log(n + 1.) - lbeta(n - k + 1., k + 1.);
51
    return -log(n + 1.) - lbeta(n - k + 1., k + 1.);
46
}
52
}
47
/* mathematically the same:
53
/* mathematically the same:
Line 64... Line 70...
64
    k = floor(k + 0.5);
70
    k = floor(k + 0.5);
65
#ifdef IEEE_754
71
#ifdef IEEE_754
66
    /* NaNs propagated correctly */
72
    /* NaNs propagated correctly */
67
    if(ISNAN(n) || ISNAN(k)) return n + k;
73
    if(ISNAN(n) || ISNAN(k)) return n + k;
68
#endif
74
#endif
-
 
75
#ifndef MATHLIB_STANDALONE
-
 
76
    R_CheckStack();
-
 
77
#endif
69
    if (fabs(k - k0) > 1e-7)
78
    if (fabs(k - k0) > 1e-7)
70
	MATHLIB_WARNING2(_("'k' (%.2f) must be integer, rounded to %.0f"), k0, k);
79
	MATHLIB_WARNING2(_("'k' (%.2f) must be integer, rounded to %.0f"), k0, k);
71
    if (k < 2) {
80
    if (k < 2) {
72
	if (k <	 0) return ML_NEGINF;
81
	if (k <	 0) return ML_NEGINF;
73
	if (k == 0) return 0.;
82
	if (k == 0) return 0.;
Line 104... Line 113...
104
    k = floor(k + 0.5);
113
    k = floor(k + 0.5);
105
#ifdef IEEE_754
114
#ifdef IEEE_754
106
    /* NaNs propagated correctly */
115
    /* NaNs propagated correctly */
107
    if(ISNAN(n) || ISNAN(k)) return n + k;
116
    if(ISNAN(n) || ISNAN(k)) return n + k;
108
#endif
117
#endif
-
 
118
#ifndef MATHLIB_STANDALONE
-
 
119
    R_CheckStack();
-
 
120
#endif
109
    if (fabs(k - k0) > 1e-7)
121
    if (fabs(k - k0) > 1e-7)
110
	MATHLIB_WARNING2(_("'k' (%.2f) must be integer, rounded to %.0f"), k0, k);
122
	MATHLIB_WARNING2(_("'k' (%.2f) must be integer, rounded to %.0f"), k0, k);
111
    if (k < k_small_max) {
123
    if (k < k_small_max) {
112
	int j;
124
	int j;
113
	if(n-k < k && n >= 0 && R_IS_INT(n)) k = n-k; /* <- Symmetry */
125
	if(n-k < k && n >= 0 && R_IS_INT(n)) k = n-k; /* <- Symmetry */