All files / math/base/special/factorial2f/lib main.js

100% Statements 94/94
100% Branches 16/16
100% Functions 1/1
100% Lines 94/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 142x 142x 142x 142x 142x 142x 142x 1x 1x 142x 44x 44x 142x 41x 41x 56x 142x 1x 1x 142x 28x 142x 27x 27x 55x 142x 784x 784x 784x 55x 142x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
var isEvenf = require( '@stdlib/math/base/assert/is-evenf' );
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var PINF = require( '@stdlib/constants/float32/pinf' );
var FLOAT32_MAX_SAFE_NTH_DOUBLE_FACTORIAL = require( '@stdlib/constants/float32/max-safe-nth-double-factorial' ); // eslint-disable-line id-length
 
 
// MAIN //
 
/**
* Evaluates the double factorial of `n` as a single-precision floating-point number.
*
* @param {number} n - input value
* @returns {(NonNegativeInteger|number)} double factorial
*
* @example
* var v = factorial2f( 3 );
* // returns 3
*
* @example
* var v = factorial2f( 4 );
* // returns 8
*
* @example
* var v = factorial2f( 57 );
* // returns Infinity
*
* @example
* var v = factorial2f( -10 );
* // returns NaN
*
* @example
* var v = factorial2f( NaN );
* // returns NaN
*/
function factorial2f( n ) {
	var last;
	var out;
	var v;
	var i;
 
	if ( isnanf( n ) ) {
		return NaN;
	}
	if ( n > FLOAT32_MAX_SAFE_NTH_DOUBLE_FACTORIAL ) {
		return PINF;
	}
	if ( n < 0 || isIntegerf( n ) === false ) {
		return NaN;
	}
	v = n|0; // asm type annotation
	if ( v === 0|0 || v === 1|0 ) {
		return 1|0; // asm type annotation
	}
	if ( isEvenf( v ) ) {
		last = 2|0; // asm type annotation
	} else {
		last = 3|0; // asm type annotation
	}
	out = float64ToFloat32( 1 );
	for ( i = v|0; i >= last; i -= 2|0 ) {
		out *= i|0; // asm type annotation
		out = float64ToFloat32( out );
	}
	return float64ToFloat32( out );
}
 
 
// EXPORTS //
 
module.exports = factorial2f;