Skip to content

Commit 82d5b0a

Browse files
committed
fix: update C files with correct implementation
--- 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 48c08b3 commit 82d5b0a

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

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

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

1919
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
20-
#include <math.h>
21-
#include <stdio.h>
20+
#include "stdlib/math/base/special/ceil.h"
2221
#include <stdlib.h>
22+
#include <stdint.h>
23+
#include <stdio.h>
24+
#include <math.h>
2325
#include <time.h>
2426
#include <sys/time.h>
2527

@@ -75,14 +77,15 @@ static double tic( void ) {
7577
}
7678

7779
/**
78-
* Generates a random integer on the interval [min,max].
80+
* Generates a random number on the interval [min,max).
7981
*
8082
* @param min minimum value (inclusive)
81-
* @param max maximum value (inclusive)
82-
* @return random integer
83+
* @param max maximum value (exclusive)
84+
* @return random number
8385
*/
84-
static int random_integer( const int min, const int max ) {
85-
return min + rand() % ( max - min + 1 );
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) );
8689
}
8790

8891
/**
@@ -92,17 +95,17 @@ static int random_integer( const int min, const int max ) {
9295
*/
9396
static double benchmark( void ) {
9497
double elapsed;
95-
double N[ 100 ];
96-
double K[ 100 ];
97-
double n[ 100 ];
98+
int32_t N[ 100 ];
99+
int32_t K[ 100 ];
100+
int32_t n[ 100 ];
98101
double v;
99102
double t;
100103
int i;
101104

102105
for ( i = 0; i < 100; i++ ) {
103-
N[ i ] = (double)random_integer( 10, 100 );
104-
K[ i ] = (double)random_integer( 1, (int)N[ i ] );
105-
n[ i ] = (double)random_integer( 1, (int)N[ i ] );
106+
N[ i ] = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
107+
K[ i ] = stdlib_base_ceil( random_uniform( 0.0, N[ i ] ) );
108+
n[ i ] = stdlib_base_ceil( random_uniform( 0.0, N[ i ] ) );
106109
}
107110

108111
t = tic();

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/round.h"
2019
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
21-
#include <stdio.h>
20+
#include "stdlib/math/base/special/ceil.h"
2221
#include <stdlib.h>
22+
#include <stdint.h>
23+
#include <stdio.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-
double K;
32-
double n;
31+
int32_t N;
32+
int32_t K;
33+
int32_t n;
3334
double v;
3435
int i;
3536

3637
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 ) );
38+
N = stdlib_base_ceil( random_uniform( 2.0, 100.0 ) );
39+
K = stdlib_base_ceil( random_uniform( 0.0, N ) );
40+
n = stdlib_base_ceil( random_uniform( 0.0, N ) );
4041
v = stdlib_base_dists_hypergeometric_variance( N, K, n );
41-
printf( "N: %lf, K: %lf, n: %lf, Var(X;N,K,n): %lf\n", N, K, n, v );
42+
printf( "N: %d, K: %d, n: %d, Var(X;N,K,n): %lf\n", N, K, n, v );
4243
}
4344
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/include/stdlib/stats/base/dists/hypergeometric/variance.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_VARIANCE_H
2020
#define STDLIB_STATS_BASE_DISTS_HYPERGEOMETRIC_VARIANCE_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
*/
@@ -27,14 +29,9 @@ extern "C" {
2729
#endif
2830

2931
/**
30-
* Returns the variance of a hypergeometric distribution with population size `N`, subpopulation size `K`, and number of draws `n`.
31-
*
32-
* @param N population size
33-
* @param K subpopulation size
34-
* @param n number of draws
35-
* @return variance of the distribution
32+
* Returns the variance of a hypergeometric distribution.
3633
*/
37-
double stdlib_base_dists_hypergeometric_variance( const double N, const double K, const double n );
34+
double stdlib_base_dists_hypergeometric_variance( const int32_t N, const int32_t K, const int32_t n );
3835

3936
#ifdef __cplusplus
4037
}

lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/src/addon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/napi/ternary.h"
2019
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
20+
#include "stdlib/math/base/napi/ternary.h"
2121

2222
// cppcheck-suppress shadowFunction
23-
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_dists_hypergeometric_variance )
23+
STDLIB_MATH_BASE_NAPI_MODULE_III_D( stdlib_base_dists_hypergeometric_variance )

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/constants/float64/pinf.h"
20-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
2119
#include "stdlib/stats/base/dists/hypergeometric/variance.h"
20+
#include "stdlib/constants/float64/pinf.h"
21+
#include <stdint.h>
2222

2323
/**
2424
* Returns the variance of a hypergeometric distribution.
2525
*
26-
* @param N population size
27-
* @param K subpopulation size
28-
* @param n number of draws
29-
* @return variance
26+
* @param N population size
27+
* @param K subpopulation size
28+
* @param n number of draws
29+
* @return variance
3030
*
3131
* @example
3232
* double v = stdlib_base_dists_hypergeometric_variance( 16, 11, 4 );
3333
* // returns ~0.688
3434
*/
35-
double stdlib_base_dists_hypergeometric_variance( const double N, const double K, const double n ) {
36-
if ( !stdlib_base_is_nonnegative_integer( N ) || !stdlib_base_is_nonnegative_integer( K ) || !stdlib_base_is_nonnegative_integer( n ) || N == STDLIB_CONSTANT_FLOAT64_PINF || K == STDLIB_CONSTANT_FLOAT64_PINF || K > N || n > N ) {
37-
return NAN;
35+
double stdlib_base_dists_hypergeometric_variance( const int32_t N, const int32_t K, const int32_t n ) {
36+
if ( N < 0 || K < 0 || n < 0 || N == STDLIB_CONSTANT_FLOAT64_PINF || K == STDLIB_CONSTANT_FLOAT64_PINF || K > N || n > N ) {
37+
return 0.0/0.0; // NaN
3838
}
39-
40-
return n * ( K / N ) * ( ( N - K ) / N ) * ( ( N - n ) / ( N - 1 ) );
39+
double p = (double)K / (double)N;
40+
double result = n * p * ( 1.0 - p ) * ( (double)(N-n) / (double)(N-1) );
41+
return result;
4142
}

0 commit comments

Comments
 (0)