-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add math/base/special/factorialf
#10032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 7 commits
a4bc58d
838ed64
b16219c
215e1cd
7534bf4
faa5fee
49608f5
289d989
d62ea4d
64067c2
5dae202
8e4a109
24bacc7
f67f8c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"> | ||
|
|
||
| * * * | ||
|
|
||
| ## 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) { | ||
| 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' ); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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 ) { | ||||||
| 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 ) { | ||||||
| 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(); | ||||||
| }); | ||||||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also section.