|
| 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 --> |
0 commit comments