Skip to content

Commit 137d3e7

Browse files
committed
feat: add complete 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 6dc82d7 commit 137d3e7

File tree

22 files changed

+283
-491
lines changed

22 files changed

+283
-491
lines changed

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

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ limitations under the License.
1818
1919
-->
2020

21-
# factorial2
21+
# factorial2f
2222

23-
> [Double factorial][double-factorial] function.
23+
> Evaluate the [double factorial][double-factorial] function as a single-precision floating-point number.
2424
2525
<section class="intro">
2626

@@ -37,36 +37,48 @@ Thus, for example, `5!!` is `5 * 3 * 1 = 15` and `8!!` is `8 * 6 * 4 * 2 = 384`.
3737
## Usage
3838

3939
```javascript
40-
var factorial2 = require( '@stdlib/math/base/special/factorial2' );
40+
var factorial2f = require( '@stdlib/math/base/special/factorial2f' );
4141
```
4242

43-
#### factorial2( n )
43+
#### factorial2f( n )
4444

45-
Evaluates the [double factorial][double-factorial] of `n`.
45+
Evaluates the [double factorial][double-factorial] of `n` as a single-precision floating-point number.
4646

4747
```javascript
48-
var v = factorial2( 2 );
49-
// returns 2
50-
51-
v = factorial2( 3 );
48+
v = factorial2f( 3 );
5249
// returns 3
5350

54-
v = factorial2( 0 );
55-
// returns 1
56-
57-
v = factorial2( 4 );
51+
v = factorial2f( 4 );
5852
// returns 8
5953

60-
v = factorial2( 5 );
61-
// returns 15
54+
v = factorial2f( 10 );
55+
// returns 3840
56+
```
6257

63-
v = factorial2( NaN );
64-
// returns NaN
58+
If `n > 56`, the function returns `NaN`, as larger [double factorial][double-factorial] values cannot be safely represented in [single-precision floating-point format][ieee754].
6559

66-
v = factorial2( 301 );
60+
```javascript
61+
var v = factorial2f( 57 );
6762
// returns Infinity
6863
```
6964

65+
If not provided a nonnegative integer value, the function returns `NaN`.
66+
67+
```javascript
68+
var v = factorial2f( 3.14 );
69+
// returns NaN
70+
71+
v = factorial2f( -1 );
72+
// returns NaN
73+
```
74+
75+
If provided `NaN`, the function returns `NaN`.
76+
77+
```javascript
78+
var v = factorial2f( NaN );
79+
// returns NaN
80+
```
81+
7082
</section>
7183

7284
<!-- /.usage -->
@@ -78,15 +90,15 @@ v = factorial2( 301 );
7890
<!-- eslint no-undef: "error" -->
7991

8092
```javascript
81-
var oneTo = require( '@stdlib/array/base/one-to' );
82-
var factorial2 = require( '@stdlib/math/base/special/factorial2' );
93+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
94+
var logEachMap = require( '@stdlib/console/log-each-map' );
95+
var factorial2f = require( '@stdlib/math/base/special/factorial2f' );
8396

84-
var values = oneTo( 300 );
97+
var x = discreteUniform( 10, 0, 56, {
98+
'dtype': 'int32'
99+
});
85100

86-
var i;
87-
for ( i = 0; i < values.length; i++ ) {
88-
console.log( 'f(%d): %d', values[ i ], factorial2( values[ i ] ) );
89-
}
101+
logEachMap( 'factorial2f(%d) = %0.1f', x, factorial2f );
90102
```
91103

92104
</section>
@@ -116,24 +128,24 @@ for ( i = 0; i < values.length; i++ ) {
116128
### Usage
117129

118130
```c
119-
#include "stdlib/math/base/special/factorial2.h"
131+
#include "stdlib/math/base/special/factorial2f.h"
120132
```
121133

122-
#### stdlib_base_factorial2( n )
134+
#### stdlib_base_factorial2f( n )
123135

124-
Evaluates the [double factorial][double-factorial] of `n`.
136+
Evaluates the [double factorial][double-factorial] of `n` as a single-precision floating-point number.
125137

126138
```c
127-
double out = stdlib_base_factorial2( 3 );
128-
// returns 3
139+
float out = stdlib_base_factorial2f( 3 );
140+
// returns 3.0f
129141
```
130142

131143
The function accepts the following arguments:
132144

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

135147
```c
136-
double stdlib_base_factorial2( const int32_t n );
148+
float stdlib_base_factorial2f( const int32_t n );
137149
```
138150
139151
</section>
@@ -155,18 +167,18 @@ double stdlib_base_factorial2( const int32_t n );
155167
### Examples
156168
157169
```c
158-
#include "stdlib/math/base/special/factorial2.h"
170+
#include "stdlib/math/base/special/factorial2f.h"
159171
#include <stdio.h>
160172
#include <stdint.h>
161173
162174
int main( void ) {
163-
const int32_t x[] = { 1, 10, 1, 301, 302 };
175+
const int32_t x[] = { 1, 10, 50, 56, 57 };
164176
165-
double b;
177+
float b;
166178
int i;
167-
for ( i = 0; i < 5; i++ ){
168-
b = stdlib_base_factorial2( x[ i ] );
169-
printf ( "factorial2(%d) = %lf\n", x[ i ], b );
179+
for ( i = 0; i < 5; i++ ) {
180+
b = stdlib_base_factorial2f( x[ i ] );
181+
printf ( "factorial2f(%d) = %f\n", x[ i ], b );
170182
}
171183
}
172184
```
@@ -183,12 +195,6 @@ int main( void ) {
183195

184196
<section class="related">
185197

186-
* * *
187-
188-
## See Also
189-
190-
- <span class="package-name">[`@stdlib/math/base/special/factorial`][@stdlib/math/base/special/factorial]</span><span class="delimiter">: </span><span class="description">evaluate a factorial.</span>
191-
192198
</section>
193199

194200
<!-- /.related -->
@@ -199,9 +205,9 @@ int main( void ) {
199205

200206
[double-factorial]: https://en.wikipedia.org/wiki/Double_factorial
201207

202-
<!-- <related-links> -->
208+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
203209

204-
[@stdlib/math/base/special/factorial]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factorial
210+
<!-- <related-links> -->
205211

206212
<!-- </related-links> -->
207213

lib/node_modules/@stdlib/math/base/special/factorial2f/benchmark/benchmark.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2023 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,26 +21,32 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2526
var pkg = require( './../package.json' ).name;
26-
var factorial2 = require( './../lib' );
27+
var factorial2f = require( './../lib' );
2728

2829

2930
// MAIN //
3031

3132
bench( pkg, function benchmark( b ) {
33+
var x;
3234
var y;
3335
var i;
3436

37+
x = discreteUniform( 100, 0, 56, {
38+
'dtype': 'int32'
39+
});
40+
3541
b.tic();
3642
for ( i = 0; i < b.iterations; i++ ) {
37-
y = factorial2( i%301 );
38-
if ( isnan( y ) ) {
43+
y = factorial2f( x[ i%x.length ] );
44+
if ( isnanf( y ) ) {
3945
b.fail( 'should not return NaN' );
4046
}
4147
}
4248
b.toc();
43-
if ( isnan( y ) ) {
49+
if ( isnanf( y ) ) {
4450
b.fail( 'should not return NaN' );
4551
}
4652
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/math/base/special/factorial2f/benchmark/benchmark.native.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,34 +22,40 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2627
var tryRequire = require( '@stdlib/utils/try-require' );
2728
var pkg = require( './../package.json' ).name;
2829

2930

3031
// VARIABLES //
3132

32-
var factorial2 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var factorial2f = tryRequire( resolve( __dirname, './../lib/native.js' ) );
3334
var opts = {
34-
'skip': ( factorial2 instanceof Error )
35+
'skip': ( factorial2f instanceof Error )
3536
};
3637

3738

3839
// MAIN //
3940

4041
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
4143
var y;
4244
var i;
4345

46+
x = discreteUniform( 100, 0, 56, {
47+
'dtype': 'int32'
48+
});
49+
4450
b.tic();
4551
for ( i = 0; i < b.iterations; i++ ) {
46-
y = factorial2( i % 301 );
47-
if ( isnan( y ) ) {
52+
y = factorial2f( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
4854
b.fail( 'should not return NaN' );
4955
}
5056
}
5157
b.toc();
52-
if ( isnan( y ) ) {
58+
if ( isnanf( y ) ) {
5359
b.fail( 'should not return NaN' );
5460
}
5561
b.pass( 'benchmark finished' );

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -16,15 +16,15 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/factorial2.h"
19+
#include "stdlib/math/base/special/factorial2f.h"
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222
#include <stdint.h>
2323
#include <math.h>
2424
#include <time.h>
2525
#include <sys/time.h>
2626

27-
#define NAME "factorial2"
27+
#define NAME "factorial2f"
2828
#define ITERATIONS 1000000
2929
#define REPEATS 3
3030

@@ -80,9 +80,9 @@ static double tic( void ) {
8080
*
8181
* @return random number
8282
*/
83-
static double rand_double( void ) {
83+
static float rand_float( void ) {
8484
int r = rand();
85-
return (double)r / ( (double)RAND_MAX + 1.0 );
85+
return (float)r / ( (float)RAND_MAX + 1.0f );
8686
}
8787

8888
/**
@@ -91,16 +91,19 @@ static double rand_double( void ) {
9191
* @return elapsed time in seconds
9292
*/
9393
static double benchmark( void ) {
94+
int32_t x[ 100 ];
9495
double elapsed;
95-
int32_t x;
96-
double y;
9796
double t;
97+
float y;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = (int32_t)( 56*rand_float() );
102+
}
103+
100104
t = tic();
101105
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = (int32_t)(rand_double() * 301);
103-
y = stdlib_base_factorial2( x );
106+
y = stdlib_base_factorial2f( x[ i%100 ] );
104107
if ( y != y ) {
105108
printf( "should not return NaN\n" );
106109
break;

0 commit comments

Comments
 (0)