Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
234 changes: 234 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/factorialf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# factorialf

> Evaluate the [Factorial][factorial-function] function of a single-precision floating-point number.

<section class="intro">

The [factorial][factorial-function] function may be defined as the product

```math
n! = \prod_{k=1}^n k
```

or according to the recurrence relation

```math
n! = \begin{cases}1 & \textrm{if } n = 0,\\(n-1)! \times n & \textrm{if } n > 1\end{cases}
```

Following the convention for an [empty product][empty-product], in all definitions,

```math
0! = 1
```

The [Gamma][@stdlib/math/base/special/gamma] function extends the [factorial][factorial-function] function for non-integer values.

```math
n! = \Gamma(n+1)
```

The [factorial][factorial-function] of a **negative** integer is not defined.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var factorialf = require( '@stdlib/math/base/special/factorialf' );
```

#### factorialf( x )

Evaluates the [factorial][factorial-function] function of a single-precision floating-point number.

```javascript
var v = factorialf( 3.0 );
// returns 6.0

v = factorialf( -1.5 );
// returns ~-3.545

v = factorialf( -0.5 );
// returns ~1.772

v = factorialf( 0.5 );
// returns ~0.886

v = factorialf( -10.0 );
// returns NaN

v = factorialf( 34.0 );
// returns ~2.952e38

v = factorialf( 35.0 );
// returns Infinity

v = factorialf( NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var incrspace = require( '@stdlib/array/base/incrspace' );
var factorialf = require( '@stdlib/math/base/special/factorialf' );

var x = incrspace( -10.0, 50.0, 1.0 );

var i;
for ( i = 0; i < x.length; i++ ) {
console.log( 'x: %d, f(x): %d', x[ i ], factorialf( x[ i ] ) );
}
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/factorialf.h"
```

#### stdlib_base_factorialf( x )

Evaluates the [factorial][factorial-function] function of a single-precision floating-point number.

```c
float out = stdlib_base_factorialf( 3.0f );
// returns 6.0f

out = stdlib_base_factorialf( -1.5f );
// returns ~-3.545f
```

The function accepts the following arguments:

- **x**: `[in] float` input value.

```c
float stdlib_base_factorialf( const float x );
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```c
#include "stdlib/math/base/special/factorialf.h"
#include <stdio.h>

int main( void ) {
const float x[] = { 2.0f, 3.0f, 5.0f, 8.0f };

float y;
int i;
for ( i = 0; i < 4; i++ ) {
y = stdlib_base_factorialf( x[ i ] );
printf( "factorialf(%f) = %f\n", x[ i ], y );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<section class="related">

* * *
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this section as it is auto generated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also section.


## See Also

- <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>
- <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>

</section>

<!-- /.related -->

<section class="links">

[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma

[factorial-function]: https://en.wikipedia.org/wiki/Factorial

[empty-product]: https://en.wikipedia.org/wiki/Empty_product

<!-- <related-links> -->

[@stdlib/math/base/special/factorial]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factorial
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related links section is also auto-generated


[@stdlib/math/base/special/factoriallnf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factoriallnf

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pkg = require( './../package.json' ).name;
var factorialf = require( './../lib' );


// MAIN //

bench( pkg+'::integers', function benchmark( b ) {

Check warning on line 31 in lib/node_modules/@stdlib/math/base/special/factorialf/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have moved away from string concatenation to string interpolation. The formatting change is described in #8647. Applies here and below.

var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = factorialf( i%35 );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::decimals', function benchmark( b ) {

Check warning on line 50 in lib/node_modules/@stdlib/math/base/special/factorialf/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = factorialf( (i%35)*0.68163 );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var isnanf = require( '@stdlib/math/base/assert/is-nan' );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isnanf = require( '@stdlib/math/base/assert/is-nan' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );

var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var factorialf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( factorialf instanceof Error )
};


// MAIN //

bench( pkg+'::native,integers', opts, function benchmark( b ) {

Check warning on line 40 in lib/node_modules/@stdlib/math/base/special/factorialf/benchmark/benchmark.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = factorialf( i % 171 );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::native,decimals', opts, function benchmark( b ) {

Check warning on line 59 in lib/node_modules/@stdlib/math/base/special/factorialf/benchmark/benchmark.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = factorialf( ( i % 171 ) * 0.68163 );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading