Skip to content

Commit 2d3fe36

Browse files
committed
refactor: modify C implemenation to accept double instead of int32
--- 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: passed - 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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 dab9a78 commit 2d3fe36

File tree

7 files changed

+35
-36
lines changed

7 files changed

+35
-36
lines changed

lib/node_modules/@stdlib/math/base/special/factorial2/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ for ( i = 0; i < values.length; i++ ) {
124124
Evaluates the [double factorial][double-factorial] of `n`.
125125

126126
```c
127-
double out = stdlib_base_factorial2( 3 );
128-
// returns 3
127+
double out = stdlib_base_factorial2( 3.0 );
128+
// returns 3.0
129129
```
130130

131131
The function accepts the following arguments:
132132

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

135135
```c
136-
double stdlib_base_factorial2( const int32_t n );
136+
double stdlib_base_factorial2( const double n );
137137
```
138138
139139
</section>
@@ -157,16 +157,15 @@ double stdlib_base_factorial2( const int32_t n );
157157
```c
158158
#include "stdlib/math/base/special/factorial2.h"
159159
#include <stdio.h>
160-
#include <stdint.h>
161160
162161
int main( void ) {
163-
const int32_t x[] = { 1, 10, 100, 301, 302 };
162+
const double x[] = { 1.0, 10.0, 100.0, 301.0, 302.0 };
164163
165164
double b;
166165
int i;
167166
for ( i = 0; i < 5; i++ ){
168167
b = stdlib_base_factorial2( x[ i ] );
169-
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
168+
printf ( "factorial2(%lf) = %lf\n", x[ i ], b );
170169
}
171170
}
172171
```

lib/node_modules/@stdlib/math/base/special/factorial2/benchmark/c/native/benchmark.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ static double rand_double( void ) {
9292
*/
9393
static double benchmark( void ) {
9494
double elapsed;
95-
int32_t x;
95+
double x;
9696
double y;
9797
double t;
9898
int i;
9999

100100
t = tic();
101101
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = (int32_t)(rand_double() * 301);
102+
x = round( rand_double() * 301 );
103103
y = stdlib_base_factorial2( x );
104104
if ( y != y ) {
105105
printf( "should not return NaN\n" );

lib/node_modules/@stdlib/math/base/special/factorial2/examples/c/example.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
#include "stdlib/math/base/special/factorial2.h"
2020
#include <stdio.h>
21-
#include <stdint.h>
2221

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

2625
double b;
2726
int i;
2827
for ( i = 0; i < 5; i++ ) {
2928
b = stdlib_base_factorial2( x[ i ] );
30-
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
29+
printf ( "factorial2(%lf) = %lf\n", x[ i ], b );
3130
}
3231
}

lib/node_modules/@stdlib/math/base/special/factorial2/include/stdlib/math/base/special/factorial2.h

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

22-
#include <stdint.h>
23-
2422
/*
2523
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2624
*/
@@ -31,7 +29,7 @@ extern "C" {
3129
/**
3230
* Evaluates the double factorial of `n`.
3331
*/
34-
double stdlib_base_factorial2( const int32_t n );
32+
double stdlib_base_factorial2( const double n );
3533

3634
#ifdef __cplusplus
3735
}

lib/node_modules/@stdlib/math/base/special/factorial2/manifest.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/math/base/assert/is-even",
4141
"@stdlib/constants/float64/pinf",
42-
"@stdlib/constants/float64/max-nth-double-factorial"
42+
"@stdlib/constants/float64/max-nth-double-factorial",
43+
"@stdlib/math/base/assert/is-integer"
4344
]
4445
},
4546
{
@@ -55,7 +56,8 @@
5556
"dependencies": [
5657
"@stdlib/math/base/assert/is-even",
5758
"@stdlib/constants/float64/pinf",
58-
"@stdlib/constants/float64/max-nth-double-factorial"
59+
"@stdlib/constants/float64/max-nth-double-factorial",
60+
"@stdlib/math/base/assert/is-integer"
5961
]
6062
},
6163
{
@@ -71,7 +73,8 @@
7173
"dependencies": [
7274
"@stdlib/math/base/assert/is-even",
7375
"@stdlib/constants/float64/pinf",
74-
"@stdlib/constants/float64/max-nth-double-factorial"
76+
"@stdlib/constants/float64/max-nth-double-factorial",
77+
"@stdlib/math/base/assert/is-integer"
7578
]
7679
}
7780
]

lib/node_modules/@stdlib/math/base/special/factorial2/src/addon.c

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

22-
STDLIB_MATH_BASE_NAPI_MODULE_I_D( stdlib_base_factorial2 )
22+
STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_factorial2 )

lib/node_modules/@stdlib/math/base/special/factorial2/src/main.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@
1818

1919
#include "stdlib/math/base/special/factorial2.h"
2020
#include "stdlib/math/base/assert/is_even.h"
21+
#include "stdlib/math/base/assert/is_integer.h"
2122
#include "stdlib/constants/float64/pinf.h"
2223
#include "stdlib/constants/float64/max_nth_double_factorial.h"
23-
#include <stdint.h>
2424

2525
/**
2626
* Evaluates the double factorial of `n`.
2727
*
28-
* @param x input value
28+
* @param n input value
2929
* @return double factorial
3030
*
3131
* @example
32-
* double v = stdlib_base_factorial2( 3 );
33-
* // returns 3
32+
* double v = stdlib_base_factorial2( 3.0 );
33+
* // returns 3.0
3434
*/
35-
double stdlib_base_factorial2( const int32_t n ) {
36-
int32_t last;
35+
double stdlib_base_factorial2( const double n ) {
36+
double last;
3737
double out;
38-
int32_t v;
39-
int32_t i;
38+
double v;
39+
double i;
40+
if ( !stdlib_base_is_integer( n ) || n < 0.0 ) {
41+
return 0.0 / 0.0; // NaN
42+
}
4043
if ( n > STDLIB_CONSTANT_FLOAT64_MAX_NTH_DOUBLE_FACTORIAL ) {
4144
return STDLIB_CONSTANT_FLOAT64_PINF;
4245
}
43-
if ( n < 0 ) {
44-
return 0.0/0.0;
45-
}
4646
v = n;
47-
if ( v == 0 || v == 1 ) {
48-
return 1;
47+
if ( v == 0.0 || v == 1.0 ) {
48+
return 1.0;
4949
}
5050
if ( stdlib_base_is_even( v ) ) {
51-
last = 2;
51+
last = 2.0;
5252
} else {
53-
last = 3;
53+
last = 3.0;
5454
}
55-
out = 1;
56-
for ( i = v; i >= last; i -= 2 ) {
55+
out = 1.0;
56+
for ( i = v; i >= last; i -= 2.0 ) {
5757
out *= i;
5858
}
5959
return out;

0 commit comments

Comments
 (0)