Skip to content

Commit 0cd519e

Browse files
committed
refactor: use int32_t instead of double for hypergeometric distribution parameters
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 8d1fd51 commit 0cd519e

File tree

7 files changed

+55
-82
lines changed

7 files changed

+55
-82
lines changed

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,18 @@ for ( i = 0; i < 10; i++ ) {
170170
Returns the mode of a hypergeometric distribution.
171171

172172
```c
173-
double out = stdlib_base_dists_hypergeometric_mode( 16.0, 11.0, 4.0 );
173+
double out = stdlib_base_dists_hypergeometric_mode( 16, 11, 4 );
174174
// returns 3.0
175175
```
176176

177177
The function accepts the following arguments:
178178

179-
- **N**: `[in] double` population size.
180-
- **K**: `[in] double` subpopulation size.
181-
- **n**: `[in] double` number of draws.
179+
- **N**: `[in] int32_t` population size.
180+
- **K**: `[in] int32_t` subpopulation size.
181+
- **n**: `[in] int32_t` number of draws.
182182

183183
```c
184-
double stdlib_base_dists_hypergeometric_mode( const double N, const double K, const double n );
184+
double stdlib_base_dists_hypergeometric_mode( const int32_t N, const int32_t K, const int32_t n );
185185
```
186186
187187
</section>
@@ -204,28 +204,28 @@ double stdlib_base_dists_hypergeometric_mode( const double N, const double K, co
204204
205205
```c
206206
#include "stdlib/stats/base/dists/hypergeometric/mode.h"
207-
#include "stdlib/math/base/special/round.h"
208207
#include <stdlib.h>
209208
#include <stdio.h>
209+
#include <stdint.h>
210210
211-
static double random_uniform( const double min, const double max ) {
212-
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
213-
return min + ( v*(max-min) );
211+
static int32_t random_int( const int32_t min, const int32_t max ) {
212+
int32_t v = rand() % ( max - min + 1 );
213+
return min + v;
214214
}
215215
216216
int main( void ) {
217-
double N;
218-
double K;
219-
double n;
217+
int32_t N;
218+
int32_t K;
219+
int32_t n;
220220
double y;
221221
int i;
222222
223223
for ( i = 0; i < 10; i++ ) {
224-
N = stdlib_base_round(random_uniform(0.0, 20.0));
225-
K = stdlib_base_round(random_uniform(0.0, N));
226-
n = stdlib_base_round(random_uniform(0.0, K));
227-
y = stdlib_base_dists_hypergeometric_mode(N, K, n);
228-
printf("N: %.0f, K: %.0f, n: %.0f, mode(X;N,K,n): %.4f\n", N, K, n, y);
224+
N = random_int( 1, 20 );
225+
K = random_int( 0, N );
226+
n = random_int( 0, K );
227+
y = stdlib_base_dists_hypergeometric_mode( N, K, n );
228+
printf( "N: %d, K: %d, n: %d, mode(X;N,K,n): %.4f\n", N, K, n, y );
229229
}
230230
}
231231
```

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/benchmark/c/benchmark.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/hypergeometric/mode.h"
20-
#include "stdlib/math/base/special/ceil.h"
21-
#include "stdlib/math/base/special/floor.h"
2220
#include <stdlib.h>
2321
#include <stdio.h>
22+
#include <stdint.h>
2423
#include <math.h>
2524
#include <time.h>
2625
#include <sys/time.h>
@@ -77,15 +76,15 @@ static double tic( void ) {
7776
}
7877

7978
/**
80-
* Generates a random number on the interval [min,max).
79+
* Generates a random integer on the interval [min,max].
8180
*
8281
* @param min minimum value (inclusive)
83-
* @param max maximum value (exclusive)
84-
* @return random number
82+
* @param max maximum value (inclusive)
83+
* @return random integer
8584
*/
86-
static double random_uniform( const double min, const double max ) {
87-
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
88-
return min + ( v*(max-min) );
85+
static int32_t random_int( const int32_t min, const int32_t max ) {
86+
int32_t v = rand() % ( max - min + 1 );
87+
return min + v;
8988
}
9089

9190
/**
@@ -95,17 +94,17 @@ static double random_uniform( const double min, const double max ) {
9594
*/
9695
static double benchmark( void ) {
9796
double elapsed;
98-
double N[ 100 ];
99-
double K[ 100 ];
100-
double n[ 100 ];
97+
int32_t N[ 100 ];
98+
int32_t K[ 100 ];
99+
int32_t n[ 100 ];
101100
double y;
102101
double t;
103102
int i;
104103

105104
for ( i = 0; i < 100; i++ ) {
106-
N[i] = stdlib_base_ceil(random_uniform(1.0, 100.0));
107-
K[i] = stdlib_base_floor(random_uniform(0.0, N[i]));
108-
n[i] = stdlib_base_floor(random_uniform(0.0, N[i]));
105+
N[i] = random_int( 1, 100 );
106+
K[i] = random_int( 0, N[i] );
107+
n[i] = random_int( 0, N[i] );
109108
}
110109

111110
t = tic();

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/examples/c/example.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/hypergeometric/mode.h"
20-
#include "stdlib/math/base/special/round.h"
2120
#include <stdlib.h>
2221
#include <stdio.h>
22+
#include <stdint.h>
2323

24-
static double random_uniform( const double min, const double max ) {
25-
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
26-
return min + ( v*(max-min) );
24+
static int32_t random_int( const int32_t min, const int32_t max ) {
25+
int32_t v = rand() % ( max - min + 1 );
26+
return min + v;
2727
}
2828

2929
int main( void ) {
30-
double N;
31-
double K;
32-
double n;
33-
double y;
34-
int i;
30+
int32_t N;
31+
int32_t K;
32+
int32_t n;
33+
double y;
34+
int i;
3535

3636
for ( i = 0; i < 10; i++ ) {
37-
N = stdlib_base_round(random_uniform(0.0, 20.0));
38-
K = stdlib_base_round(random_uniform(0.0, N));
39-
n = stdlib_base_round(random_uniform(0.0, K));
40-
y = stdlib_base_dists_hypergeometric_mode(N, K, n);
41-
printf("N: %.0f, K: %.0f, n: %.0f, mode(X;N,K,n): %.4f\n", N, K, n, y);
37+
N = random_int( 1, 20 );
38+
K = random_int( 0, N );
39+
n = random_int( 0, K );
40+
y = stdlib_base_dists_hypergeometric_mode( N, K, n );
41+
printf( "N: %d, K: %d, n: %d, mode(X;N,K,n): %.4f\n", N, K, n, y );
4242
}
4343
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/include/stdlib/stats/base/dists/hypergeometric/mode.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_HYPERGEOMETRIC_MODE_H
2020
#define STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_MODE_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 mode of a hypergeometric distribution.
3133
*/
32-
double stdlib_base_dists_hypergeometric_mode( const double N, const double K, const double n );
34+
double stdlib_base_dists_hypergeometric_mode( const int32_t N, const int32_t K, const int32_t n );
3335

3436
#ifdef __cplusplus
3537
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/manifest.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838
"libraries": [],
3939
"libpath": [],
4040
"dependencies": [
41-
"@stdlib/math/base/assert/is-nonnegative-integer",
4241
"@stdlib/math/base/napi/ternary",
43-
"@stdlib/math/base/assert/is-nan",
44-
"@stdlib/constants/float64/pinf",
4542
"@stdlib/math/base/special/floor"
4643
]
4744
},
@@ -57,11 +54,7 @@
5754
"libraries": [],
5855
"libpath": [],
5956
"dependencies": [
60-
"@stdlib/math/base/assert/is-nonnegative-integer",
61-
"@stdlib/math/base/assert/is-nan",
62-
"@stdlib/math/base/special/ceil",
63-
"@stdlib/math/base/special/floor",
64-
"@stdlib/constants/float64/pinf"
57+
"@stdlib/math/base/special/floor"
6558
]
6659
},
6760
{
@@ -76,10 +69,6 @@
7669
"libraries": [],
7770
"libpath": [],
7871
"dependencies": [
79-
"@stdlib/math/base/assert/is-nonnegative-integer",
80-
"@stdlib/math/base/special/round",
81-
"@stdlib/math/base/assert/is-nan",
82-
"@stdlib/constants/float64/pinf",
8372
"@stdlib/math/base/special/floor"
8473
]
8574
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/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_hypergeometric_mode )
23+
STDLIB_MATH_BASE_NAPI_MODULE_III_D( stdlib_base_dists_hypergeometric_mode )

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/mode/src/main.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/hypergeometric/mode.h"
20-
#include "stdlib/math/base/assert/is_nan.h"
21-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
22-
#include "stdlib/constants/float64/pinf.h"
2320
#include "stdlib/math/base/special/floor.h"
21+
#include <stdint.h>
2422

2523
/**
2624
* Returns the mode of a hypergeometric distribution.
@@ -31,27 +29,12 @@
3129
* @return mode
3230
*
3331
* @example
34-
* double v = stdlib_base_dists_hypergeometric_mode( 16.0, 11.0, 4.0 );
32+
* double v = stdlib_base_dists_hypergeometric_mode( 16, 11, 4 );
3533
* // returns 3.0
3634
*/
37-
double stdlib_base_dists_hypergeometric_mode( const double N, const double K, const double n ) {
38-
if (
39-
stdlib_base_is_nan( N) ||
40-
stdlib_base_is_nan( K ) ||
41-
stdlib_base_is_nan( n )
42-
) {
43-
return 0.0 / 0.0; // NaN
35+
double stdlib_base_dists_hypergeometric_mode( const int32_t N, const int32_t K, const int32_t n ) {
36+
if ( N < 0 || K < 0 || n < 0 || K > N || n > N ) {
37+
return 0.0/0.0; // NaN
4438
}
45-
if (
46-
!stdlib_base_is_nonnegative_integer(N) ||
47-
!stdlib_base_is_nonnegative_integer(K) ||
48-
!stdlib_base_is_nonnegative_integer(n) ||
49-
N == STDLIB_CONSTANT_FLOAT64_PINF ||
50-
K == STDLIB_CONSTANT_FLOAT64_PINF ||
51-
K > N ||
52-
n > N
53-
) {
54-
return 0.0 / 0.0; // NaN
55-
}
56-
return stdlib_base_floor( ((n + 1.0) * (K + 1.0)) / (N + 2.0) );
39+
return stdlib_base_floor( ( ( (double)n + 1.0 ) * ( (double)K + 1.0 ) ) / ( (double)N + 2.0 ) );
5740
}

0 commit comments

Comments
 (0)