All files main.js

100% Statements 146/146
100% Branches 13/13
100% Functions 1/1
100% Lines 146/146

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 1471x 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 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 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 55671x 2x 2x 55671x 2x 2x 55671x 35637x 35637x 35637x 35637x 55671x 1179x 1179x 1179x 1179x 20030x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 18851x 55671x 10323x 10323x 55667x 55671x 1x 1x 1x 1x 1x  
/**
* @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.
*
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link https://www.boost.org/doc/libs/1_92_0/boost/math/special_functions/detail/bessel_j1.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* Copyright Xiaogang Zhang, 2006.
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* ```
*/
 
'use strict';
 
// MODULES //
 
var sqrtf = require( '@stdlib/math/base/special/sqrtf' );
var absf = require( '@stdlib/math/base/special/absf' );
var sincosf = require( '@stdlib/math/base/special/sincosf' ).assign;
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var PINF = require( '@stdlib/constants/float32/pinf' );
var FLOAT32_SQRT_PI = require( '@stdlib/constants/float32/sqrt-pi' );
var poly1 = require( './rational_p1q1.js' );
var poly2 = require( './rational_p2q2.js' );
var polyC = require( './rational_pcqc.js' );
var polyS = require( './rational_psqs.js' );
 
 
// VARIABLES //
 
var x1 = f32( 3.8317059702075123156e+00 );
var x2 = f32( 7.0155866698156187535e+00 );
var x11 = f32( 9.810e+02 );
var x12 = f32( -3.2527979248768438556e-04 );
var x21 = f32( 1.7960e+03 );
var x22 = f32( -3.8330184381246462950e-05 );
 
// `sincosf` workspace:
var sc = [ 0.0, 0.0 ];
 
 
// MAIN //
 
/**
* Computes the Bessel function of the first kind of order one for a single-precision floating-point number.
*
* @param {number} x - input value
* @returns {number} evaluated Bessel function
*
* @example
* var v = besselj1f( 0.0 );
* // returns 0.0
*
* @example
* var v = besselj1f( 1.0 );
* // returns ~0.440
*
* @example
* var v = besselj1f( Infinity );
* // returns 0.0
*
* @example
* var v = besselj1f( -Infinity );
* // returns 0.0
*
* @example
* var v = besselj1f( NaN );
* // returns NaN
*/
function besselj1f( x ) {
	var value;
	var rc;
	var rs;
	var y2;
	var r;
	var y;
	var f;
	var w;
 
	x = f32( x );
	w = absf( x );
	if ( x === 0.0 ) {
		return 0.0;
	}
	if ( w === PINF ) {
		return 0.0;
	}
	if ( w <= 4.0 ) {
		y = f32( x * x );
		r = f32( poly1( y ) );
		f = f32( f32( w * f32( w + x1 ) ) * f32( f32( w - f32( x11 / 256.0 ) ) - x12 ) ); // eslint-disable-line max-len
		value = f32( f * r );
	} else if ( w <= 8.0 ) {
		y = f32( x * x );
		r = f32( poly2( y ) );
		f = f32( f32( w * f32( w + x2 ) ) * f32( f32( w - f32( x21 / 256.0 ) ) - x22 ) ); // eslint-disable-line max-len
		value = f32( f * r );
	} else {
		y = f32( 8.0 / w );
		y2 = f32( y * y );
		rc = f32( polyC( y2 ) );
		rs = f32( polyS( y2 ) );
		f = f32( 1.0 / f32( sqrtf( w ) * FLOAT32_SQRT_PI ) );
 
		/*
		* What follows is really just:
		*
		* ```
		* z = w - 0.75 * pi;
		* value = f * ( rc * cos( z ) - y * rs * sin( z ) );
		* ```
		*
		* but using the sin/cos addition rules plus constants for the values of sin/cos of `3π/4` which then cancel out with corresponding terms in "f".
		*/
		sincosf( w, sc, 1, 0 );
		value = f32( f * f32( f32( rc * f32( sc[0] - sc[1] ) ) + f32( f32( y * rs ) * f32( sc[0] + sc[1] ) ) ) ); // eslint-disable-line max-len
	}
	if ( x < 0.0 ) {
		value = f32( -value );
	}
	return value;
}
 
 
// EXPORTS //
 
module.exports = besselj1f;