Skip to content

Commit 4eaa59f

Browse files
feat: add math/base/special/factorial2f
PR-URL: #6736 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 72d1412 commit 4eaa59f

File tree

26 files changed

+2014
-0
lines changed

26 files changed

+2014
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# factorial2f
22+
23+
> Evaluate the [double factorial][double-factorial] function as a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [double factorial][double-factorial] of a number `n`, denoted `n!!`, is defined as the product of all the positive integers up to `n` that have the same parity (odd or even) as `n`.
28+
29+
Thus, for example, `5!!` is `5 * 3 * 1 = 15` and `8!!` is `8 * 6 * 4 * 2 = 384`.
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var factorial2f = require( '@stdlib/math/base/special/factorial2f' );
41+
```
42+
43+
#### factorial2f( n )
44+
45+
Evaluates the [double factorial][double-factorial] of `n` as a single-precision floating-point number.
46+
47+
```javascript
48+
var v = factorial2f( 3 );
49+
// returns 3
50+
51+
v = factorial2f( 4 );
52+
// returns 8
53+
54+
v = factorial2f( 10 );
55+
// returns 3840
56+
```
57+
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].
59+
60+
```javascript
61+
var v = factorial2f( 57 );
62+
// returns Infinity
63+
```
64+
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+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
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' );
96+
97+
var x = discreteUniform( 10, 0, 56, {
98+
'dtype': 'int32'
99+
});
100+
101+
logEachMap( 'factorial2f(%d) = %0.1f', x, factorial2f );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- C interface documentation. -->
109+
110+
* * *
111+
112+
<section class="c">
113+
114+
## C APIs
115+
116+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
117+
118+
<section class="intro">
119+
120+
</section>
121+
122+
<!-- /.intro -->
123+
124+
<!-- C usage documentation. -->
125+
126+
<section class="usage">
127+
128+
### Usage
129+
130+
```c
131+
#include "stdlib/math/base/special/factorial2f.h"
132+
```
133+
134+
#### stdlib_base_factorial2f( n )
135+
136+
Evaluates the [double factorial][double-factorial] of `n` as a single-precision floating-point number.
137+
138+
```c
139+
float out = stdlib_base_factorial2f( 3 );
140+
// returns 3.0f
141+
```
142+
143+
The function accepts the following arguments:
144+
145+
- **n**: `[in] int32_t` input value.
146+
147+
```c
148+
float stdlib_base_factorial2f( const int32_t n );
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="notes">
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<!-- C API usage examples. -->
164+
165+
<section class="examples">
166+
167+
### Examples
168+
169+
```c
170+
#include "stdlib/math/base/special/factorial2f.h"
171+
#include <stdio.h>
172+
#include <stdint.h>
173+
174+
int main( void ) {
175+
const int32_t x[] = { 1, 10, 50, 56, 57 };
176+
177+
float b;
178+
int i;
179+
for ( i = 0; i < 5; i++ ) {
180+
b = stdlib_base_factorial2f( x[ i ] );
181+
printf ( "factorial2f(%d) = %f\n", x[ i ], b );
182+
}
183+
}
184+
```
185+
186+
</section>
187+
188+
<!-- /.examples -->
189+
190+
</section>
191+
192+
<!-- /.c -->
193+
194+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
195+
196+
<section class="related">
197+
198+
</section>
199+
200+
<!-- /.related -->
201+
202+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
203+
204+
<section class="links">
205+
206+
[double-factorial]: https://en.wikipedia.org/wiki/Double_factorial
207+
208+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
209+
210+
<!-- <related-links> -->
211+
212+
<!-- </related-links> -->
213+
214+
</section>
215+
216+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var pkg = require( './../package.json' ).name;
27+
var factorial2f = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = discreteUniform( 100, 0, 56, {
38+
'dtype': 'int32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = factorial2f( x[ i%x.length ] );
44+
if ( isnanf( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var factorial2f = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( factorial2f instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = discreteUniform( 100, 0, 56, {
47+
'dtype': 'int32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = factorial2f( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)