Skip to content

Commit 74da505

Browse files
committed
fix: correct critical parameter type issues in binomial/mgf
CRITICAL FIXES: - Fix C function signature: n parameter from double to int32_t - Fix C header declaration to match implementation - Fix NAPI macro from DDD_D to DID_D for correct type binding - Fix README C API documentation (parameter types and signatures) - Fix C examples and benchmarks to use int32_t for n parameter - Add missing stdint.h includes RATIONALE: - The number of trials (n) in binomial distribution must be integer - This matches other stdlib binomial implementations - Fixes parameter validation and type consistency - Prevents accepting invalid non-integer trial counts All C code now correctly uses int32_t for discrete count parameter.
1 parent 3a88859 commit 74da505

File tree

6 files changed

+22
-21
lines changed

6 files changed

+22
-21
lines changed

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ double out = stdlib_base_dists_binomial_mgf( 0.5, 20, 0.2 );
194194

195195
The function accepts the following arguments:
196196

197-
- **t**: `[in] int` input value.
198-
- **n**: `[in] int` number of trials.
197+
- **t**: `[in] double` input value.
198+
- **n**: `[in] int32_t` number of trials.
199199
- **p**: `[in] double` success probability.
200200

201201
```c
202-
double stdlib_base_dists_binomial_mgf( const double t, const double n, const double p );
202+
double stdlib_base_dists_binomial_mgf( const double t, const int32_t n, const double p );
203203
```
204204
205205
</section>
@@ -232,18 +232,18 @@ static double random_uniform( const double min, const double max ) {
232232
}
233233
234234
int main( void ) {
235-
double n;
235+
int32_t n;
236236
double p;
237237
double t;
238238
double y;
239239
int i;
240240
241241
for ( i = 0; i < 25; i++ ) {
242-
n = stdlib_base_ceil( random_uniform( 0, 100 ) );
242+
n = (int32_t)stdlib_base_ceil( random_uniform( 0, 100 ) );
243243
p = random_uniform( 0, 1 );
244244
t = random_uniform( 0, 5 );
245245
y = stdlib_base_dists_binomial_mgf( t, n, p );
246-
printf( "t: %lf, n: %lf, p: %lf, M_X(t;n,p): %lf\n", t, n, p, y );
246+
printf( "t: %lf, n: %d, p: %lf, M_X(t;n,p): %lf\n", t, n, p, y );
247247
}
248248
249249
return 0;

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/benchmark/c/benchmark.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <math.h>
2424
#include <time.h>
2525
#include <sys/time.h>
26+
#include <stdint.h>
2627

2728
#define NAME "binomial-mgf"
2829
#define ITERATIONS 1000000
@@ -94,15 +95,15 @@ static double random_uniform( const double min, const double max ) {
9495
*/
9596
static double benchmark( void ) {
9697
double elapsed;
97-
double n[ 100 ];
98+
int32_t n[ 100 ];
9899
double p[ 100 ];
99100
double t[ 100 ];
100101
double y;
101102
double tc;
102103
int i;
103104

104105
for ( i = 0; i < 100; i++ ) {
105-
n[ i ] = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
106+
n[ i ] = (int32_t)stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
106107
p[ i ] = random_uniform( 0.0, 1.0 );
107108
t[ i ] = random_uniform( 0.0, 5.0);
108109
}

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/examples/c/example.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,26 @@
2020
#include "stdlib/math/base/special/ceil.h"
2121
#include <stdlib.h>
2222
#include <stdio.h>
23+
#include <stdint.h>
2324

2425
static double random_uniform( const double min, const double max ) {
2526
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
2627
return min + ( v * (max - min) );
2728
}
2829

2930
int main( void ) {
30-
double n;
31+
int32_t n;
3132
double p;
3233
double t;
3334
double y;
3435
int i;
3536

3637
for ( i = 0; i < 25; i++ ) {
37-
n = stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
38+
n = (int32_t)stdlib_base_ceil( random_uniform( 0.0, 100.0 ) );
3839
p = random_uniform( 0.0, 1.0 );
3940
t = random_uniform( 0.0, 5.0 );
4041
y = stdlib_base_dists_binomial_mgf( t, n, p );
41-
printf( "t: %lf, n: %lf, p: %lf, M_X(t;n,p): %lf\n", t, n, p, y );
42+
printf( "t: %lf, n: %d, p: %lf, M_X(t;n,p): %lf\n", t, n, p, y );
4243
}
4344

4445
return 0;

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/include/stdlib/stats/base/dists/binomial/mgf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_MGF_H
2020
#define STDLIB_STATS_BASE_DISTS_BINOMIAL_MGF_H
2121

22+
#include <stdint.h>
23+
2224
/*
2325
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2426
*/
@@ -29,7 +31,7 @@ extern "C" {
2931
/**
3032
* Returns the moment-generating function (MGF) of a binomial distribution.
3133
*/
32-
double stdlib_base_dists_binomial_mgf( const double t, const double n, const double p );
34+
double stdlib_base_dists_binomial_mgf( const double t, const int32_t n, const double p );
3335

3436
#ifdef __cplusplus
3537
}

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/src/addon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
#include "stdlib/math/base/napi/ternary.h"
2121

2222
// cppcheck-suppress shadowFunction
23-
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_binomial_mgf );
23+
STDLIB_MATH_BASE_NAPI_MODULE_DID_D( stdlib_base_dists_binomial_mgf );

lib/node_modules/@stdlib/stats/base/dists/binomial/mgf/src/main.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "stdlib/stats/base/dists/binomial/mgf.h"
2020
#include "stdlib/math/base/assert/is_nan.h"
2121
#include "stdlib/constants/float64/pinf.h"
22-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
2322
#include "stdlib/math/base/special/pow.h"
2423
#include "stdlib/math/base/special/exp.h"
24+
#include <stdint.h>
2525

2626
/**
2727
* Returns the moment-generating function (MGF) of a binomial distribution.
@@ -35,18 +35,15 @@
3535
* double y = stdlib_base_dists_binomial_mgf( 0.5, 20, 0.2 );
3636
* // returns ~11.471
3737
*/
38-
double stdlib_base_dists_binomial_mgf( const double t, const double n, const double p ) {
38+
double stdlib_base_dists_binomial_mgf( const double t, const int32_t n, const double p ) {
3939
if (
4040
stdlib_base_is_nan( t ) ||
41-
stdlib_base_is_nan( n ) ||
4241
stdlib_base_is_nan( p ) ||
43-
n < 0.0 ||
42+
n < 0 ||
4443
p < 0.0 ||
45-
p > 1.0 ||
46-
!stdlib_base_is_nonnegative_integer( n ) ||
47-
n == STDLIB_CONSTANT_FLOAT64_PINF
44+
p > 1.0
4845
) {
4946
return 0.0 / 0.0;
5047
}
51-
return stdlib_base_pow( ( 1.0 - p + ( p*stdlib_base_exp( t ) ) ), n );
48+
return stdlib_base_pow( ( 1.0 - p + ( p*stdlib_base_exp( t ) ) ), (double)n );
5249
}

0 commit comments

Comments
 (0)