Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions lib/node_modules/@stdlib/math/base/special/binomcoef/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ logEachMap( '%d choose %d = %d', n, k, binomcoef );
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k`.

```c
double v = stdlib_base_binomcoef( 8, 2 );
double v = stdlib_base_binomcoef( 8.0, 2.0 );
// returns 28.0
```

The function accepts the following arguments:

- **n**: `[in] int64_t` input value.
- **k**: `[in] int64_t` input value.
- **n**: `[in] double` input value.
- **k**: `[in] double` input value.

```c
double stdlib_base_binomcoef( const int64_t n, const int64_t k );
double stdlib_base_binomcoef( const double n, const double k );
```
</section>
Expand All @@ -206,18 +206,16 @@ double stdlib_base_binomcoef( const int64_t n, const int64_t k );
```c
#include "stdlib/math/base/special/binomcoef.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main( void ) {
const int64_t a[] = { 24, 32, 48, 116, 33 };
const int64_t b[] = { 12, 6, 15, 52, 22 };
const double a[] = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b[] = { 12.0, 6.0, 15.0, 52.0, 22.0 };
double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_binomcoef( a[ i ], b[ i ] );
printf( "binomcoef(%" PRId64 ", %" PRId64 ") = %lf\n", a[ i ], b[ i ], out );
printf( "binomcoef(%lf, %lf) = %lf\n", a[ i ], b[ i ], out );
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
int64_t n[ 100 ];
int64_t k[ 100 ];
double n[ 100 ];
double k[ 100 ];
double elapsed;
double y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
n[ i ] = (int64_t)round( 500.0 * rand_double() );
k[ i ] = (int64_t)round( 500.0 * rand_double() );
n[ i ] = round( 500.0 * rand_double() );
k[ i ] = round( 500.0 * rand_double() );
}

t = tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@

#include "stdlib/math/base/special/binomcoef.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main( void ) {
const int64_t a[] = { 24, 32, 48, 116, 33 };
const int64_t b[] = { 12, 6, 15, 52, 22 };
const double a[] = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b[] = { 12.0, 6.0, 15.0, 52.0, 22.0 };

double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_binomcoef( a[ i ], b[ i ] );
printf( "binomcoef(%" PRId64 ", %" PRId64 ") = %lf\n", a[ i ], b[ i ], out );
printf( "binomcoef(%lf, %lf) = %lf\n", a[ i ], b[ i ], out );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#ifndef STDLIB_MATH_BASE_SPECIAL_BINOMCOEF_H
#define STDLIB_MATH_BASE_SPECIAL_BINOMCOEF_H

#include <stdint.h>

/*
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
*/
Expand All @@ -31,7 +29,7 @@ extern "C" {
/**
* Computes the binomial coefficient of two integers.
*/
double stdlib_base_binomcoef( const int64_t n, const int64_t k );
double stdlib_base_binomcoef( const double n, const double k );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/gcd",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-safe-integer"
"@stdlib/constants/float64/max-safe-integer",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-odd"
]
},
{
Expand All @@ -61,7 +63,9 @@
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/gcd",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-safe-integer"
"@stdlib/constants/float64/max-safe-integer",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-odd"
]
},
{
Expand All @@ -80,7 +84,9 @@
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/gcd",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-safe-integer"
"@stdlib/constants/float64/max-safe-integer",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-odd"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
#include "stdlib/math/base/special/binomcoef.h"
#include "stdlib/math/base/napi/binary.h"

STDLIB_MATH_BASE_NAPI_MODULE_LL_D( stdlib_base_binomcoef )
STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_binomcoef )
25 changes: 15 additions & 10 deletions lib/node_modules/@stdlib/math/base/special/binomcoef/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
*/

#include "stdlib/math/base/special/binomcoef.h"
#include "stdlib/math/base/assert/is_integer.h"
#include "stdlib/math/base/assert/is_odd.h"
#include "stdlib/math/base/special/floor.h"
#include "stdlib/math/base/special/gcd.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/constants/float64/max_safe_integer.h"
#include <stdint.h>

/**
* Computes the binomial coefficient of two integers.
Expand All @@ -31,28 +32,32 @@
* @return function value
*
* @example
* double out = stdlib_base_binomcoef( 8, 2 );
* double out = stdlib_base_binomcoef( 8.0, 2.0 );
* // returns 28.0
*/
double stdlib_base_binomcoef( const int64_t n, const int64_t k ) {
double stdlib_base_binomcoef( const double n, const double k ) {
double res;
double sgn;
int64_t nc;
int64_t kc;
int64_t d;
double nc;
double kc;
double d;
double b;
double c;
double g;
double s;

if ( !stdlib_base_is_integer( n ) || !stdlib_base_is_integer( k ) ) {
return 0.0 / 0.0; // NaN
}

if ( k < 0 ) {
return 0.0;
}
sgn = 1.0;
nc = n;
if ( nc < 0 ) {
nc = -nc + k - 1;
if ( k & 1 ) {
if ( stdlib_base_is_odd( k ) ) {
sgn *= -1.0;
}
}
Expand All @@ -63,19 +68,19 @@ double stdlib_base_binomcoef( const int64_t n, const int64_t k ) {
return sgn;
}
if ( k == 1 || k == nc - 1 ) {
return sgn * (double)nc;
return sgn * nc;
}

// Minimize the number of computed terms by leveraging symmetry:
kc = k;
if ( nc - kc < kc ) {
kc = nc - kc;
}
s = stdlib_base_floor( (double)STDLIB_CONSTANT_FLOAT64_MAX_SAFE_INTEGER / (double)nc );
s = stdlib_base_floor( (double)STDLIB_CONSTANT_FLOAT64_MAX_SAFE_INTEGER / nc );

// Use a standard algorithm for computing the binomial coefficient
res = 1.0;
for ( d = 1; d <= kc; d++ ) {
for ( d = 1.0; d <= kc; d++ ) {
// Check for potential overflow...
if ( res > s ) {
break;
Expand Down