Skip to content

Commit d051592

Browse files
committed
feat: add JS implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 237978f commit d051592

File tree

4 files changed

+187
-18
lines changed

4 files changed

+187
-18
lines changed

lib/node_modules/@stdlib/math/base/special/kernel-cosf/lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
'use strict';
2020

2121
/**
22-
* Compute the cosine of a number on `[-π/4, π/4]`.
22+
* Compute the cosine of a single-precision floating-point number on `[-π/4, π/4]`.
2323
*
2424
* @module @stdlib/math/base/special/kernel-cosf
2525
*
2626
* @example
2727
* var kernelCosf = require( '@stdlib/math/base/special/kernel-cosf' );
2828
*
29-
* var v = kernelCosf( 0.0, 0.0 );
29+
* var v = kernelCosf( 0.0 );
3030
* // returns ~1.0
3131
*
32-
* v = kernelCosf( 3.141592653589793/6.0, 0.0 );
32+
* v = kernelCosf( 3.141592653589793/6.0 );
3333
* // returns ~0.866
3434
*
35-
* v = kernelCosf( 0.785, -1.144e-17 );
35+
* v = kernelCosf( 0.785 );
3636
* // returns ~0.707
3737
*
38-
* v = kernelCosf( NaN, 0.0 );
38+
* v = kernelCosf( NaN );
3939
* // returns NaN
4040
*/
4141

lib/node_modules/@stdlib/math/base/special/kernel-cosf/lib/main.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,55 @@
3232

3333
'use strict';
3434

35+
// MODULES //
36+
37+
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
38+
var polyval23 = require( './polyval_c23.js' );
39+
40+
3541
// VARIABLES //
3642

37-
var C0 = -0.499999997251031003120 // -0x1ffffffd0c5e81.0p-54
38-
var C1 = 0.0416666233237390631894 // 0x155553e1053a42.0p-57
39-
var C2 = -0.00138867637746099294692 // -0x16c087e80f1e27.0p-62
40-
var C3 = 0.0000243904487962774090654 // 0x199342e0ee5069.0p-68
43+
var C0 = -0.499999997251031003120; // -0x1ffffffd0c5e81.0p-54
44+
var C1 = 0.0416666233237390631894; // 0x155553e1053a42.0p-57
4145

4246

4347
// MAIN //
4448

4549
/**
46-
* Computes the cosine on \\( \[-\pi/4, \pi/4] \\), where \\( \pi/4 \approx 0.785398164 \\).
50+
* Computes the cosine of a single-precision floating-point number on \\( \[-\pi/4, \pi/4] \\), where \\( \pi/4 \approx 0.785398164 \\).
4751
*
4852
* ## Notes
4953
*
50-
* - \\( | \cos(x) - c(x) | \\) < 2^{-34.1} \approx \\( \[ -5.37 \times 10^{-11}, 5.295 \times 10^{-11} ] \\).
54+
* - \\( | \cos(x) - c(x) | < 2^{-34.1} \\), where \\( 2^{-34.1} \approx \[ -5.37 \times 10^{-11}, 5.295 \times 10^{-11} ] \\).
5155
*
5256
* @param {number} x - input value (in radians, assumed to be bounded by ~pi/4 in magnitude)
5357
* @returns {number} cosine
5458
*
5559
* @example
56-
* var v = kernelCosf( 0.0, 0.0 );
60+
* var v = kernelCosf( 0.0 );
5761
* // returns ~1.0
5862
*
5963
* @example
60-
* var v = kernelCosf( 3.141592653589793/6.0, 0.0 );
64+
* var v = kernelCosf( 3.141592653589793/6.0 );
6165
* // returns ~0.866
6266
*
6367
* @example
64-
* var v = kernelCosf( 0.785, -1.144e-17 );
68+
* var v = kernelCosf( 0.785 );
6569
* // returns ~0.707
6670
*
6771
* @example
68-
* var v = kernelCosf( NaN, 0.0 );
72+
* var v = kernelCosf( NaN );
6973
* // returns NaN
7074
*/
71-
function kernelCosf( x, y ) {
75+
function kernelCosf( x ) {
7276
var r;
7377
var w;
7478
var z;
7579

7680
z = x * x;
7781
w = z * z;
78-
r = C2 + ( z*C3 );
79-
return ( (( 1.0+z*C0 ) + w*C1) + (w*z)*r );
82+
r = polyval23( z );
83+
return float64ToFloat32( (( 1.0+(z*C0) ) + (w*C1)) + ((w*z)*r) );
8084
}
8185

8286

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* This is a generated file. Do not edit directly. */
20+
'use strict';
21+
22+
// MAIN //
23+
24+
/**
25+
* Evaluates a polynomial.
26+
*
27+
* ## Notes
28+
*
29+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
30+
*
31+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
32+
*
33+
* @private
34+
* @param {number} x - value at which to evaluate the polynomial
35+
* @returns {number} evaluated polynomial
36+
*/
37+
function evalpoly( x ) {
38+
if ( x === 0.0 ) {
39+
return -0.001388676377460993;
40+
}
41+
return -0.001388676377460993 + (x * 0.00002439044879627741);
42+
}
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = evalpoly;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/*
20+
* This script compiles modules for evaluating polynomial functions. If any polynomial coefficients change, this script should be rerun to update the compiled files.
21+
*/
22+
'use strict';
23+
24+
// MODULES //
25+
26+
var resolve = require( 'path' ).resolve;
27+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
28+
var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
29+
var currentYear = require( '@stdlib/time/current-year' );
30+
var substringBefore = require( '@stdlib/string/substring-before' );
31+
var substringAfter = require( '@stdlib/string/substring-after' );
32+
var format = require( '@stdlib/string/format' );
33+
var licenseHeader = require( '@stdlib/_tools/licenses/header' );
34+
var compile = require( '@stdlib/math/base/tools/evalpoly-compile' );
35+
var compileC = require( '@stdlib/math/base/tools/evalpoly-compile-c' );
36+
37+
38+
// VARIABLES //
39+
40+
// Polynomial coefficients ordered in ascending degree...
41+
var C23 = [
42+
-0.00138867637746099294692, // -0x16c087e80f1e27.0p-62
43+
0.0000243904487962774090654 // 0x199342e0ee5069.0p-68
44+
];
45+
46+
// Header to add to output files:
47+
var header = licenseHeader( 'Apache-2.0', 'js', {
48+
'year': currentYear(),
49+
'copyright': 'The Stdlib Authors'
50+
});
51+
header += '\n/* This is a generated file. Do not edit directly. */\n';
52+
53+
54+
// FUNCTIONS //
55+
56+
/**
57+
* Inserts a compiled function into file content.
58+
*
59+
* @private
60+
* @param {string} text - source content
61+
* @param {string} id - function identifier
62+
* @param {string} str - function string
63+
* @returns {string} updated content
64+
*/
65+
function insert( text, id, str ) {
66+
var before;
67+
var after;
68+
var begin;
69+
var end;
70+
71+
begin = '// BEGIN: '+id;
72+
end = '// END: '+id;
73+
74+
before = substringBefore( text, begin );
75+
after = substringAfter( text, end );
76+
77+
return format( '%s// BEGIN: %s\n\n%s\n%s%s', before, id, str, end, after );
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var fpath;
90+
var copts;
91+
var opts;
92+
var file;
93+
var str;
94+
95+
opts = {
96+
'encoding': 'utf8'
97+
};
98+
99+
fpath = resolve( __dirname, '..', 'lib', 'polyval_c23.js' );
100+
str = header + compile( C23 );
101+
writeFileSync( fpath, str, opts );
102+
103+
copts = {
104+
'dtype': 'double',
105+
'name': ''
106+
};
107+
108+
fpath = resolve( __dirname, '..', 'src', 'main.c' );
109+
file = readFileSync( fpath, opts );
110+
111+
copts.name = 'polyval_c23';
112+
str = compileC( C23, copts );
113+
file = insert( file, copts.name, str );
114+
115+
writeFileSync( fpath, file, opts );
116+
}
117+
118+
main();

0 commit comments

Comments
 (0)