All files / math/base/special/kernel-sinf/lib main.js

100% Statements 146/146
100% Branches 4/4
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 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 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 2003x 1001x 1001x 1002x 1002x 1002x 1002x 1002x 1002x 1002x 1002x 2003x 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.
*
*
* ## Notice
*
* The following copyright, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/k_sin.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ```
*/
 
'use strict';
 
// MODULES //
 
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
 
// VARIABLES //
 
var S1 = -1.6666667e-01;  // 0xBFC55555
var S2 = 8.3333333e-03;   // 0x3F811111
var S3 = -1.9841270e-04;  // 0xBF2A01A0
var S4 = 2.7557314e-06;   // 0x3EC71DE3
var S5 = -2.5050760e-08;  // 0xBE5AE5E6
var S6 = 1.5896910e-10;   // 0x3DE5D93A
 
 
// MAIN //
 
/**
* Computes the sine on \\( \approx \[-\pi/4, \pi/4] \\) (except on \\(-0\\)), where \\( \pi/4 \approx 0.7854 \\) in single precision.
*
* ## Method
*
* -   Since \\( \sin(-x) = -\sin(x) \\), we need only to consider positive \\(x\\).
*
* -   Callers must return \\( \sin(-0) = -0 \\) without calling here since our odd polynomial is not evaluated in a way that preserves \\(-0\\). Callers may do the optimization \\( \sin(x) \approx x \\) for tiny \\(x\\).
*
* -   \\( \sin(x) \\) is approximated by a polynomial of degree \\(13\\) on \\( \left\[0,\tfrac{pi}{4}\right] \\)
*
*     ```tex
*     \sin(x) \approx x + S_1 \cdot x^3 + \ldots + S_6 \cdot x^{13}
*     ```
*
*     where
*
*     ```tex
*     \left| \frac{\sin(x)}{x} \left( 1 + S_1 \cdot x + S_2 \cdot x + S_3 \cdot x + S_4 \cdot x + S_5 \cdot x + S_6 \cdot x \right) \right| \le 2^{-58}
*     ```
*
* -   We have
*
*     ```tex
*     \sin(x+y) = \sin(x) + \sin'(x') \cdot y \approx \sin(x) + (1-x*x/2) \cdot y
*     ```
*
*     For better accuracy, let
*
*     ```tex
*     r = x^3 * \left( S_2 + x^2 \cdot \left( S_3 + x^2 * \left( S_4 + x^2 \cdot ( S_5+x^2 \cdot S_6 ) \right) \right) \right)
*     ```
*
*     then
*
*     ```tex
*     \sin(x) = x + \left( S_1 \cdot x + ( x \cdot (r-y/2) + y ) \right)
*     ```
*
* @param {number} x - input value (in radians, assumed to be bounded by `~pi/4` in magnitude)
* @param {number} y - tail of `x`
* @returns {number} sine
*
* @example
* var v = kernelSinf( 0.0, 0.0 );
* // returns ~0.0
*
* @example
* var v = kernelSinf( 3.14159/6.0, 0.0 );
* // returns ~0.5
*
* @example
* var v = kernelSinf( 0.619, 9.279e-10 );
* // returns ~0.58
*
* @example
* var v = kernelSinf( NaN, 0.0 );
* // returns NaN
*
* @example
* var v = kernelSinf( 3.0, NaN );
* // returns NaN
*
* @example
* var v = kernelSinf( NaN, NaN );
* // returns NaN
*/
function kernelSinf( x, y ) {
	var r;
	var v;
	var w;
	var z;
 
	z = float64ToFloat32(x * x);
	w = float64ToFloat32(z * z);
	r = float64ToFloat32(S2 + float64ToFloat32(z * float64ToFloat32(S3 + float64ToFloat32(z*S4))));
	r = float64ToFloat32(r + float64ToFloat32(z * w * float64ToFloat32(S5 + float64ToFloat32(z*S6))));
	v = float64ToFloat32(z * x);
 
	if ( y === 0.0 ) {
		return float64ToFloat32(x + float64ToFloat32(v * float64ToFloat32(S1 + float64ToFloat32(z*r))));
	}
 
	var temp1 = float64ToFloat32(0.5*y);
	var temp2 = float64ToFloat32(v*r);
	var temp3 = float64ToFloat32(z*(temp1 - temp2));
	var temp4 = float64ToFloat32(v*S1);
	var temp5 = float64ToFloat32((temp3 - y) - temp4);
 
	return float64ToFloat32(x - temp5);
}
 
 
// EXPORTS //
 
module.exports = kernelSinf;