Skip to content

Commit a4bc58d

Browse files
feat(factorialf):implementation for math/base/special/factorialf
1 parent ee4e852 commit a4bc58d

33 files changed

Lines changed: 2477 additions & 0 deletions
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# factorialf
22+
23+
> [Factorial][factorial-function] function of a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [factorial][factorial-function] function may be defined as the product
28+
29+
```math
30+
n! = \prod_{k=1}^n k
31+
```
32+
33+
or according to the recurrence relation
34+
35+
```math
36+
n! = \begin{cases}1 & \textrm{if } n = 0,\\(n-1)! \times n & \textrm{if } n > 1\end{cases}
37+
```
38+
39+
Following the convention for an [empty product][empty-product], in all definitions,
40+
41+
```math
42+
0! = 1
43+
```
44+
45+
The [Gamma][@stdlib/math/base/special/gamma] function extends the [factorial][factorial-function] function for non-integer values.
46+
47+
```math
48+
n! = \Gamma(n+1)
49+
```
50+
51+
The [factorial][factorial-function] of a **negative** integer is not defined.
52+
53+
</section>
54+
55+
<!-- /.intro -->
56+
57+
<section class="usage">
58+
59+
## Usage
60+
61+
```javascript
62+
var factorialf = require( '@stdlib/math/base/special/factorialf' );
63+
```
64+
65+
#### factorialf( x )
66+
67+
Evaluates the [factorial][factorial-function] function of a single-precision floating-point number.
68+
69+
```javascript
70+
var v = factorialf( 3.0 );
71+
// returns 6.0
72+
73+
v = factorialf( -1.5 );
74+
// returns ~-3.545
75+
76+
v = factorialf( -0.5 );
77+
// returns ~1.772
78+
79+
v = factorialf( 0.5 );
80+
// returns ~0.886
81+
82+
v = factorialf( -10.0 );
83+
// returns NaN
84+
85+
v = factorialf( 34.0 );
86+
// returns ~2.952e38
87+
88+
v = factorialf( 35.0 );
89+
// returns Infinity
90+
91+
v = factorialf( NaN );
92+
// returns NaN
93+
```
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var incrspace = require( '@stdlib/array/base/incrspace' );
107+
var factorialf = require( '@stdlib/math/base/special/factorialf' );
108+
109+
var x = incrspace( -10.0, 50.0, 1.0 );
110+
111+
var i;
112+
for ( i = 0; i < x.length; i++ ) {
113+
console.log( 'x: %d, f(x): %d', x[ i ], factorialf( x[ i ] ) );
114+
}
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- C interface documentation. -->
122+
123+
* * *
124+
125+
<section class="c">
126+
127+
## C APIs
128+
129+
<section class="intro">
130+
131+
</section>
132+
133+
<!-- /.intro -->
134+
135+
<!-- C usage documentation. -->
136+
137+
<section class="usage">
138+
139+
### Usage
140+
141+
```c
142+
#include "stdlib/math/base/special/factorialf.h"
143+
```
144+
145+
#### stdlib_base_factorialf( x )
146+
147+
Evaluates the [factorial][factorial-function] function of a single-precision floating-point number.
148+
149+
```c
150+
float out = stdlib_base_factorialf( 3.0f );
151+
// returns 6.0f
152+
153+
out = stdlib_base_factorialf( -1.5f );
154+
// returns ~-3.545f
155+
```
156+
157+
The function accepts the following arguments:
158+
159+
- **x**: `[in] float` input value.
160+
161+
```c
162+
float stdlib_base_factorialf( const float x );
163+
```
164+
165+
</section>
166+
167+
<!-- /.usage -->
168+
169+
<section class="notes">
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
### Examples
178+
179+
```c
180+
#include "stdlib/math/base/special/factorialf.h"
181+
#include <stdio.h>
182+
183+
int main( void ) {
184+
const float x[] = { 2.0f, 3.0f, 5.0f, 8.0f };
185+
186+
float y;
187+
int i;
188+
for ( i = 0; i < 4; i++ ) {
189+
y = stdlib_base_factorialf( x[ i ] );
190+
printf( "factorialf(%f) = %f\n", x[ i ], y );
191+
}
192+
}
193+
```
194+
195+
</section>
196+
197+
<!-- /.examples -->
198+
199+
</section>
200+
201+
<!-- /.c -->
202+
203+
<section class="related">
204+
205+
* * *
206+
207+
## See Also
208+
209+
- <span class="package-name">[`@stdlib/math/base/special/factorial`][@stdlib/math/base/special/factorial]</span><span class="delimiter">: </span><span class="description">evaluate the factorial function.</span>
210+
- <span class="package-name">[`@stdlib/math/base/special/factoriallnf`][@stdlib/math/base/special/factoriallnf]</span><span class="delimiter">: </span><span class="description">evaluate the natural logarithm of the factorial function of a single-precision floating-point number.</span>
211+
212+
</section>
213+
214+
<!-- /.related -->
215+
216+
<section class="links">
217+
218+
[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma
219+
220+
[factorial-function]: https://en.wikipedia.org/wiki/Factorial
221+
222+
[empty-product]: https://en.wikipedia.org/wiki/Empty_product
223+
224+
<!-- <related-links> -->
225+
226+
[@stdlib/math/base/special/factorial]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factorial
227+
228+
[@stdlib/math/base/special/factoriallnf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factoriallnf
229+
230+
<!-- </related-links> -->
231+
232+
</section>
233+
234+
<!-- /.links -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 pkg = require( './../package.json' ).name;
26+
var factorialf = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+'::integers', function benchmark( b ) {
32+
var y;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
y = factorialf( i%35 );
38+
if ( isnanf( y ) ) {
39+
b.fail( 'should not return NaN' );
40+
}
41+
}
42+
b.toc();
43+
if ( isnanf( y ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+'::decimals', function benchmark( b ) {
51+
var y;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = factorialf( (i%35)*0.68163 );
57+
if ( isnanf( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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-nan' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// VARIABLES //
31+
32+
var factorialf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var opts = {
34+
'skip': ( factorialf instanceof Error )
35+
};
36+
37+
38+
// MAIN //
39+
40+
bench( pkg+'::native,integers', opts, function benchmark( b ) {
41+
var y;
42+
var i;
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
y = factorialf( i % 171 );
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnanf( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::native,decimals', opts, function benchmark( b ) {
60+
var y;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
y = factorialf( ( i % 171 ) * 0.68163 );
66+
if ( isnanf( y ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnanf( y ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});

0 commit comments

Comments
 (0)