Skip to content

Commit d34d482

Browse files
committed
test: add missing difficult cases to strided and assign tests
--- 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: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - 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 5f40372 commit d34d482

File tree

2 files changed

+412
-1
lines changed

2 files changed

+412
-1
lines changed

lib/node_modules/@stdlib/complex/float64/base/div/test/test.assign.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
var tape = require( 'tape' );
2626
var abs = require( '@stdlib/math/base/special/abs' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
2728
var EPS = require( '@stdlib/constants/float64/eps' );
2829
var PINF = require( '@stdlib/constants/float64/pinf' );
2930
var NINF = require( '@stdlib/constants/float64/ninf' );
3031
var isnan = require( '@stdlib/math/base/assert/is-nan' );
3132
var isAlmostEqualFloat64Array = require( '@stdlib/assert/is-almost-equal-float64array' );
33+
var toBinaryString = require( '@stdlib/number/float64/base/to-binary-string' );
3234
var Float64Array = require( '@stdlib/array/float64' );
3335
var cdiv = require( './../lib/assign.js' );
3436

@@ -50,6 +52,34 @@ var tinyPositiveImaginaryComponents = require( './fixtures/julia/tiny_positive_i
5052
var tinyPositiveRealComponents = require( './fixtures/julia/tiny_positive_real_components.json' );
5153

5254

55+
// FUNCTIONS //
56+
57+
/**
58+
* Compares the binary representations of two double-precision floating-point numbers and returns the first index of a differing bit. If all bits match, the function returns `-1`.
59+
*
60+
* TODO: revisit once ULP distance fcn is written
61+
*
62+
* @private
63+
* @param {number} a - first number
64+
* @param {number} b - second number
65+
* @returns {integer} index
66+
*/
67+
function bitdiff( a, b ) {
68+
var astr;
69+
var bstr;
70+
var i;
71+
72+
astr = toBinaryString( a );
73+
bstr = toBinaryString( b );
74+
for ( i = 0; i < 64; i++ ) {
75+
if ( astr[ i ] !== bstr[ i ] ) {
76+
return i;
77+
}
78+
}
79+
return -1;
80+
}
81+
82+
5383
// TESTS //
5484

5585
tape( 'main export is a function', function test( t ) {
@@ -72,6 +102,171 @@ tape( 'the function computes a complex quotient (base behavior)', function test(
72102
t.end();
73103
});
74104

105+
tape( 'the function computes a complex quotient (difficult cases)', function test( t ) {
106+
var idx;
107+
var re1;
108+
var im1;
109+
var re2;
110+
var im2;
111+
var out;
112+
var v;
113+
114+
// Note: test cases extracted from Figure 6 of https://arxiv.org/pdf/1210.4539.pdf.
115+
116+
// Test case #1:
117+
out = new Float64Array( 2 );
118+
v = cdiv( 1.0, 1.0, 1.0, pow( 2.0, 1023.0 ), out, 1, 0 );
119+
120+
idx = bitdiff( v[ 0 ], pow( 2.0, -1023.0 ) );
121+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
122+
123+
idx = bitdiff( v[ 1 ], -pow( 2.0, -1023.0 ) );
124+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
125+
126+
// Test case #2:
127+
out = new Float64Array( 2 );
128+
v = cdiv( 1.0, 1.0, pow( 2.0, -1023.0 ), pow( 2.0, -1023.0 ), out, 1, 0 );
129+
130+
idx = bitdiff( v[ 0 ], pow( 2.0, 1023.0 ) );
131+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
132+
133+
idx = bitdiff( v[ 1 ], 0.0 );
134+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
135+
136+
// Test case #3:
137+
re1 = pow( 2.0, 1023.0 );
138+
im1 = pow( 2.0, -1023.0 );
139+
re2 = pow( 2.0, 677.0 );
140+
im2 = pow( 2.0, -677.0 );
141+
142+
out = new Float64Array( 2 );
143+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
144+
145+
idx = bitdiff( v[ 0 ], pow( 2.0, 346.0 ) );
146+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
147+
148+
idx = bitdiff( v[ 1 ], -pow( 2.0, -1008.0 ) );
149+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
150+
151+
// Test case #4:
152+
out = new Float64Array( 2 );
153+
v = cdiv( pow( 2.0, 1023.0 ), pow( 2.0, 1023.0 ), 1.0, 1.0, out, 1, 0 );
154+
155+
idx = bitdiff( v[ 0 ], pow( 2.0, 1023.0 ) );
156+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
157+
158+
idx = bitdiff( v[ 1 ], 0.0 );
159+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
160+
161+
// Test case #5:
162+
re1 = pow( 2.0, 1020.0 );
163+
im1 = pow( 2.0, -844.0 );
164+
re2 = pow( 2.0, 656.0 );
165+
im2 = pow( 2.0, -780.0 );
166+
167+
out = new Float64Array( 2 );
168+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
169+
170+
idx = bitdiff( v[ 0 ], pow( 2.0, 364.0 ) );
171+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
172+
173+
idx = bitdiff( v[ 1 ], -pow( 2.0, -1072.0 ) );
174+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
175+
176+
// Test case #6:
177+
re1 = pow( 2.0, -71.0 );
178+
im1 = pow( 2.0, 1021.0 );
179+
re2 = pow( 2.0, 1001.0 );
180+
im2 = pow( 2.0, -323.0 );
181+
182+
out = new Float64Array( 2 );
183+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
184+
185+
idx = bitdiff( v[ 0 ], pow( 2.0, -1072.0 ) );
186+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
187+
188+
idx = bitdiff( v[ 1 ], pow( 2.0, 20.0 ) );
189+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
190+
191+
// Test case #7:
192+
re1 = pow( 2.0, -347.0 );
193+
im1 = pow( 2.0, -54.0 );
194+
re2 = pow( 2.0, -1037.0 );
195+
im2 = pow( 2.0, -1058.0 );
196+
197+
out = new Float64Array( 2 );
198+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
199+
200+
idx = bitdiff( v[ 0 ], 3.898125604559113300e289 );
201+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
202+
203+
idx = bitdiff( v[ 1 ], 8.174961907852353577e295 );
204+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
205+
206+
// Test case #8:
207+
re1 = pow( 2.0, -1074.0 );
208+
im1 = pow( 2.0, -1074.0 );
209+
re2 = pow( 2.0, -1073.0 );
210+
im2 = pow( 2.0, -1074.0 );
211+
212+
out = new Float64Array( 2 );
213+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
214+
215+
/*
216+
* See section 3.6 in https://arxiv.org/pdf/1210.4539.pdf.
217+
*
218+
* ```text
219+
* real(q): 0011111111100011001100110011001100110011001100110011001100110100
220+
* 0.6: 0011111111100011001100110011001100110011001100110011001100110011
221+
* ```
222+
*
223+
* If we add
224+
*
225+
* ```text
226+
* 0000000000000000000000000000000000000000000000000000000000000001
227+
* ```
228+
*
229+
* to `0.6`, we get `real( q )`; thus, the result is 1 bit off.
230+
*/
231+
idx = bitdiff( v[ 0 ], 0.6 );
232+
t.strictEqual( idx, 61, 'real component has expected binary representation' );
233+
234+
idx = bitdiff( v[ 1 ], 0.2 );
235+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
236+
237+
// Test case #9:
238+
re1 = pow( 2.0, 1015.0 );
239+
im1 = pow( 2.0, -989.0 );
240+
re2 = pow( 2.0, 1023.0 );
241+
im2 = pow( 2.0, 1023.0 );
242+
243+
out = new Float64Array( 2 );
244+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
245+
246+
idx = bitdiff( v[ 0 ], 0.001953125 );
247+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
248+
249+
idx = bitdiff( v[ 1 ], -0.001953125 );
250+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
251+
252+
// Test case #10:
253+
re1 = pow( 2.0, -622.0 );
254+
im1 = pow( 2.0, -1071.0 );
255+
re2 = pow( 2.0, -343.0 );
256+
im2 = pow( 2.0, -798.0 );
257+
258+
out = new Float64Array( 2 );
259+
v = cdiv( re1, im1, re2, im2, out, 1, 0 );
260+
261+
idx = bitdiff( v[ 0 ], 1.02951151789360578e-84 );
262+
t.strictEqual( idx, -1, 'real component has expected binary representation' );
263+
264+
idx = bitdiff( v[ 1 ], 6.97145987515076231e-220 );
265+
t.strictEqual( idx, -1, 'imaginary component has expected binary representation' );
266+
267+
t.end();
268+
});
269+
75270
tape( 'the function computes a complex quotient (tested against fixtures)', function test( t ) {
76271
var delta;
77272
var out;

0 commit comments

Comments
 (0)