Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 6 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/factorial2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ for ( i = 0; i < values.length; i++ ) {
Evaluates the [double factorial][double-factorial] of `n`.

```c
double out = stdlib_base_factorial2( 3 );
// returns 3
double out = stdlib_base_factorial2( 3.0 );
// returns 3.0
```

The function accepts the following arguments:

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

```c
double stdlib_base_factorial2( const int32_t n );
double stdlib_base_factorial2( const double n );
```

</section>
Expand All @@ -157,16 +157,15 @@ double stdlib_base_factorial2( const int32_t n );
```c
#include "stdlib/math/base/special/factorial2.h"
#include <stdio.h>
#include <stdint.h>

int main( void ) {
const int32_t x[] = { 1, 10, 100, 301, 302 };
const double x[] = { 1.0, 10.0, 100.0, 301.0, 302.0 };

double b;
int i;
for ( i = 0; i < 5; i++ ){
b = stdlib_base_factorial2( x[ i ] );
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
printf ( "factorial2(%lf) = %lf\n", x[ i ], b );
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
int32_t x;
double x;
double y;
double t;
int i;

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = (int32_t)(rand_double() * 301);
x = round( rand_double() * 301.0 );
y = stdlib_base_factorial2( x );
if ( y != y ) {
printf( "should not return NaN\n" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

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

int main( void ) {
const int32_t x[] = { 1, 10, 100, 301, 302 };
const double x[] = { 1.0, 10.0, 100.0, 301.0, 302.0 };

double b;
int i;
for ( i = 0; i < 5; i++ ) {
b = stdlib_base_factorial2( x[ i ] );
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
printf ( "factorial2(%lf) = %lf\n", x[ i ], b );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#ifndef STDLIB_MATH_BASE_SPECIAL_FACTORIAL2_H
#define STDLIB_MATH_BASE_SPECIAL_FACTORIAL2_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" {
/**
* Evaluates the double factorial of `n`.
*/
double stdlib_base_factorial2( const int32_t n );
double stdlib_base_factorial2( const double n );

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/assert/is-even",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-nth-double-factorial"
"@stdlib/constants/float64/max-nth-double-factorial",
"@stdlib/math/base/assert/is-integer"
]
},
{
Expand All @@ -55,7 +56,8 @@
"dependencies": [
"@stdlib/math/base/assert/is-even",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-nth-double-factorial"
"@stdlib/constants/float64/max-nth-double-factorial",
"@stdlib/math/base/assert/is-integer"
]
},
{
Expand All @@ -71,7 +73,8 @@
"dependencies": [
"@stdlib/math/base/assert/is-even",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/max-nth-double-factorial"
"@stdlib/constants/float64/max-nth-double-factorial",
"@stdlib/math/base/assert/is-integer"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
#include "stdlib/math/base/special/factorial2.h"
#include "stdlib/math/base/napi/unary.h"

STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_factorial2 )
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_factorial2 )
34 changes: 17 additions & 17 deletions lib/node_modules/@stdlib/math/base/special/factorial2/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@

#include "stdlib/math/base/special/factorial2.h"
#include "stdlib/math/base/assert/is_even.h"
#include "stdlib/math/base/assert/is_integer.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/constants/float64/max_nth_double_factorial.h"
#include <stdint.h>

/**
* Evaluates the double factorial of `n`.
*
* @param x input value
* @param n input value
* @return double factorial
*
* @example
* double v = stdlib_base_factorial2( 3 );
* // returns 3
* double v = stdlib_base_factorial2( 3.0 );
* // returns 3.0
*/
double stdlib_base_factorial2( const int32_t n ) {
int32_t last;
double stdlib_base_factorial2( const double n ) {
double last;
double out;
int32_t v;
int32_t i;
double v;
double i;
if ( !stdlib_base_is_integer( n ) || n < 0.0 ) {
return 0.0 / 0.0; // NaN
}
if ( n > STDLIB_CONSTANT_FLOAT64_MAX_NTH_DOUBLE_FACTORIAL ) {
return STDLIB_CONSTANT_FLOAT64_PINF;
}
if ( n < 0 ) {
return 0.0/0.0;
}
v = n;
if ( v == 0 || v == 1 ) {
return 1;
if ( v == 0.0 || v == 1.0 ) {
return 1.0;
}
if ( stdlib_base_is_even( v ) ) {
last = 2;
last = 2.0;
} else {
last = 3;
last = 3.0;
}
out = 1;
for ( i = v; i >= last; i -= 2 ) {
out = 1.0;
for ( i = v; i >= last; i -= 2.0 ) {
out *= i;
}
return out;
Expand Down