Skip to content

Commit 351f7c0

Browse files
authored
test: migrate math/base/special/sqrtpif to ULP-based testing
PR-URL: #13803 Reviewed-by: Athan Reines <kgryte@gmail.com> Ref: #11352
1 parent 95d0b24 commit 351f7c0

2 files changed

Lines changed: 52 additions & 148 deletions

File tree

lib/node_modules/@stdlib/math/base/special/sqrtpif/test/test.js

Lines changed: 26 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zerof' );
2626
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zerof' );
2727
var PINF = require( '@stdlib/constants/float32/pinf' );
2828
var NINF = require( '@stdlib/constants/float32/ninf' );
29-
var EPS = require( '@stdlib/constants/float32/eps' );
30-
var absf = require( '@stdlib/math/base/special/absf' );
29+
var isAlmostSameValuef = require( '@stdlib/number/float32/base/assert/is-almost-same-value' );
30+
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
3131
var sqrtpif = require( './../lib' );
3232

3333

@@ -53,184 +53,136 @@ tape( 'main export is a function', function test( t ) {
5353

5454
tape( 'the function evaluates the principal square root when `x` is on the interval `[50,500]`', function test( t ) {
5555
var expected;
56-
var delta;
57-
var tol;
5856
var x;
5957
var y;
58+
var e;
6059
var i;
6160

6261
expected = veryLargePositive.expected;
6362
x = veryLargePositive.x;
6463
for ( i = 0; i < x.length; i++ ) {
64+
e = float64ToFloat32( expected[i] );
6565
y = sqrtpif( x[i] );
66-
if ( y === expected[i] ) {
67-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
68-
} else {
69-
delta = absf( y - expected[i] );
70-
tol = EPS * absf( expected[i] );
71-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
72-
}
66+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
7367
}
7468
t.end();
7569
});
7670

7771
tape( 'the function evaluates the principal square root when `x` is on the interval `[20,50]`', function test( t ) {
7872
var expected;
79-
var delta;
80-
var tol;
8173
var x;
8274
var y;
75+
var e;
8376
var i;
8477

8578
expected = largePositive.expected;
8679
x = largePositive.x;
8780
for ( i = 0; i < x.length; i++ ) {
81+
e = float64ToFloat32( expected[i] );
8882
y = sqrtpif( x[i] );
89-
if ( y === expected[i] ) {
90-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
91-
} else {
92-
delta = absf( y - expected[i] );
93-
tol = EPS * absf( expected[i] );
94-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
95-
}
83+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
9684
}
9785
t.end();
9886
});
9987

10088
tape( 'the function evaluates the principal square root when `x` is on the interval `[3,20]`', function test( t ) {
10189
var expected;
102-
var delta;
103-
var tol;
10490
var x;
10591
var y;
92+
var e;
10693
var i;
10794

10895
expected = mediumPositive.expected;
10996
x = mediumPositive.x;
11097
for ( i = 0; i < x.length; i++ ) {
98+
e = float64ToFloat32( expected[i] );
11199
y = sqrtpif( x[i] );
112-
if ( y === expected[i] ) {
113-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
114-
} else {
115-
delta = absf( y - expected[i] );
116-
tol = EPS * absf( expected[i] );
117-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
118-
}
100+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
119101
}
120102
t.end();
121103
});
122104

123105
tape( 'the function evaluates the principal square root when `x` is on the interval `[0.8,3]`', function test( t ) {
124106
var expected;
125-
var delta;
126-
var tol;
127107
var x;
128108
var y;
109+
var e;
129110
var i;
130111

131112
expected = smallPositive.expected;
132113
x = smallPositive.x;
133114
for ( i = 0; i < x.length; i++ ) {
115+
e = float64ToFloat32( expected[i] );
134116
y = sqrtpif( x[i] );
135-
if ( y === expected[i] ) {
136-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
137-
} else {
138-
delta = absf( y - expected[i] );
139-
tol = EPS * absf( expected[i] );
140-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
141-
}
117+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
142118
}
143119
t.end();
144120
});
145121

146122
tape( 'the function evaluates principal square root when `x` is on the interval `[0.0,0.8]`', function test( t ) {
147123
var expected;
148-
var delta;
149-
var tol;
150124
var x;
151125
var y;
126+
var e;
152127
var i;
153128

154129
expected = smaller.expected;
155130
x = smaller.x;
156131
for ( i = 0; i < x.length; i++ ) {
132+
e = float64ToFloat32( expected[i] );
157133
y = sqrtpif( x[i] );
158-
if ( y === expected[i] ) {
159-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
160-
} else {
161-
delta = absf( y - expected[i] );
162-
tol = EPS * absf( expected[i] );
163-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
164-
}
134+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
165135
}
166136
t.end();
167137
});
168138

169139
tape( 'the function evaluates the principal square root when `x` is on the interval `[1e-300,1e-308]`', function test( t ) {
170140
var expected;
171-
var delta;
172-
var tol;
173141
var x;
174142
var y;
143+
var e;
175144
var i;
176145

177146
expected = tinyPositive.expected;
178147
x = tinyPositive.x;
179148
for ( i = 0; i < x.length; i++ ) {
149+
e = float64ToFloat32( expected[i] );
180150
y = sqrtpif( x[i] );
181-
if ( y === expected[i] ) {
182-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
183-
} else {
184-
delta = absf( y - expected[i] );
185-
tol = EPS * absf( expected[i] );
186-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
187-
}
151+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
188152
}
189153
t.end();
190154
});
191155

192156
tape( 'the function evaluates the principal square root when `x` is subnormal', function test( t ) {
193157
var expected;
194-
var delta;
195-
var tol;
196158
var x;
197159
var y;
160+
var e;
198161
var i;
199162

200163
expected = subnormal.expected;
201164
x = subnormal.x;
202165
for ( i = 0; i < x.length; i++ ) {
166+
e = float64ToFloat32( expected[i] );
203167
y = sqrtpif( x[i] );
204-
if ( y === expected[i] ) {
205-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
206-
} else {
207-
delta = absf( y - expected[i] );
208-
tol = EPS * absf( expected[i] );
209-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
210-
}
168+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
211169
}
212170
t.end();
213171
});
214172

215173
tape( 'the function evaluates the principal square root for huge positive numbers', function test( t ) {
216174
var expected;
217-
var delta;
218-
var tol;
219175
var x;
220176
var y;
177+
var e;
221178
var i;
222179

223180
expected = hugePositive.expected;
224181
x = hugePositive.x;
225182
for ( i = 0; i < x.length; i++ ) {
183+
e = float64ToFloat32( expected[i] );
226184
y = sqrtpif( x[i] );
227-
if ( y === expected[i] ) {
228-
t.strictEqual( y, expected[i], 'x: '+x[i]+', y: '+y+', expected: '+expected[i] );
229-
} else {
230-
delta = absf( y - expected[i] );
231-
tol = EPS * absf( expected[i] );
232-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. Δ: '+delta+'. tol: '+tol+'.' );
233-
}
185+
t.strictEqual( isAlmostSameValuef( y, e, 1 ), true, 'returns expected value' );
234186
}
235187
t.end();
236188
});

0 commit comments

Comments
 (0)