Skip to content

Commit e9e5c55

Browse files
committed
chore: fix JavaScript lint errors (issue #5377)
1 parent 66f9e29 commit e9e5c55

File tree

1 file changed

+121
-0
lines changed
  • lib/node_modules/@stdlib/math/base/special/sqrt/scripts

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
'use strict';
21+
22+
// MODULES //
23+
24+
var log = require( '@stdlib/console/log' );
25+
var abs = require( '@stdlib/math/base/special/abs' );
26+
var sqrt = require( './../lib' );
27+
28+
29+
// FUNCTIONS //
30+
31+
/**
32+
* Computes a rough square root approximation using a small fixed number of Newton iterations.
33+
*
34+
* @private
35+
* @param {number} x - input value
36+
* @returns {number} approximate square root
37+
*/
38+
function approxSqrt( x ) {
39+
var guess;
40+
var i;
41+
42+
if ( x < 0.0 ) {
43+
return NaN;
44+
}
45+
if ( x === 0.0 ) {
46+
return 0.0;
47+
}
48+
guess = ( x > 1.0 ) ? x / 2.0 : 1.0;
49+
for ( i = 0; i < 5; i++ ) {
50+
guess = 0.5 * ( guess + ( x / guess ) );
51+
}
52+
return guess;
53+
}
54+
55+
/**
56+
* Computes the arithmetic mean.
57+
*
58+
* @private
59+
* @param {Array<number>} values - input array
60+
* @returns {number} mean value
61+
*/
62+
function mean( values ) {
63+
var sum;
64+
var i;
65+
66+
if ( values.length === 0 ) {
67+
return NaN;
68+
}
69+
sum = 0.0;
70+
for ( i = 0; i < values.length; i++ ) {
71+
sum += values[ i ];
72+
}
73+
return sum / values.length;
74+
}
75+
76+
/**
77+
* Computes the relative error between two numbers.
78+
*
79+
* @private
80+
* @param {number} value - computed value
81+
* @param {number} expected - reference value
82+
* @returns {number} relative error
83+
*/
84+
function relativeError( value, expected ) {
85+
var denom = ( expected === 0.0 ) ? 1e-16 : expected;
86+
return abs( ( value - expected ) / denom );
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Evaluate the mean relative error between a simple Newton approximation and `@stdlib`'s sqrt implementation.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var approx;
99+
var errors;
100+
var expected;
101+
var values;
102+
var i;
103+
104+
values = [
105+
0.5,
106+
1.5,
107+
2.5,
108+
10.0,
109+
25.0,
110+
100.0
111+
];
112+
errors = [];
113+
for ( i = 0; i < values.length; i++ ) {
114+
expected = sqrt( values[ i ] );
115+
approx = approxSqrt( values[ i ] );
116+
errors.push( relativeError( approx, expected ) );
117+
}
118+
log( 'Mean relative error (approx vs @stdlib sqrt): %d', mean( errors ) );
119+
}
120+
121+
main();

0 commit comments

Comments
 (0)