Skip to content

Commit 1aa6c6d

Browse files
test: add tests for C99 edge cases
PR-URL: #6389 Reviewed-by: Athan Reines <[email protected]>
1 parent dceca5a commit 1aa6c6d

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

Diff for: lib/node_modules/@stdlib/math/base/special/cabs/lib/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ var imag = require( '@stdlib/complex/float64/imag' );
4040
* // returns ~5.83
4141
*/
4242
function cabs( z ) {
43-
// TODO: consider whether to use C99 rules for special cases involving infinities and nans (see https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Objects/complexobject.c#L191)
4443
return hypot( real( z ), imag( z ) );
4544
}
4645

Diff for: lib/node_modules/@stdlib/math/base/special/cabs/test/test.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var EPS = require( '@stdlib/constants/float64/eps' );
26+
var PINF = require( '@stdlib/constants/float64/pinf' );
27+
var NINF = require( '@stdlib/constants/float64/ninf' );
2628
var abs = require( '@stdlib/math/base/special/abs' );
2729
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2830
var cabs = require( './../lib' );
@@ -67,17 +69,59 @@ tape( 'the function computes the absolute value of a complex number', function t
6769
t.end();
6870
});
6971

70-
tape( 'if either the real or imaginary component is `NaN`, the function returns `NaN`', function test( t ) {
72+
tape( 'if either the real or imaginary component is `+infinity`, the function returns `+infinity`', function test( t ) {
73+
var v;
74+
75+
v = cabs( new Complex128( PINF, 3.0 ) );
76+
t.strictEqual( v, PINF, 'returns expected value' );
77+
78+
v = cabs( new Complex128( 5.0, PINF ) );
79+
t.strictEqual( v, PINF, 'returns expected value' );
80+
81+
v = cabs( new Complex128( PINF, PINF ) );
82+
t.strictEqual( v, PINF, 'returns expected value' );
83+
84+
v = cabs( new Complex128( NaN, PINF ) );
85+
t.strictEqual( v, PINF, 'returns expected value' );
86+
87+
v = cabs( new Complex128( PINF, NaN ) );
88+
t.strictEqual( v, PINF, 'returns expected value' );
89+
90+
t.end();
91+
});
92+
93+
tape( 'if either the real or imaginary component is `-infinity`, the function returns `+infinity`', function test( t ) {
94+
var v;
95+
96+
v = cabs( new Complex128( NINF, 3.0 ) );
97+
t.strictEqual( v, PINF, 'returns expected value' );
98+
99+
v = cabs( new Complex128( 5.0, NINF ) );
100+
t.strictEqual( v, PINF, 'returns expected value' );
101+
102+
v = cabs( new Complex128( NINF, NINF ) );
103+
t.strictEqual( v, PINF, 'returns expected value' );
104+
105+
v = cabs( new Complex128( NaN, NINF ) );
106+
t.strictEqual( v, PINF, 'returns expected value' );
107+
108+
v = cabs( new Complex128( NINF, NaN ) );
109+
t.strictEqual( v, PINF, 'returns expected value' );
110+
111+
t.end();
112+
});
113+
114+
tape( 'if either the real or imaginary component is `NaN` but not `+-infinity`, the function returns `NaN`', function test( t ) {
71115
var v;
72116

73117
v = cabs( new Complex128( NaN, 3.0 ) );
74-
t.strictEqual( isnan( v ), true, 'returns NaN' );
118+
t.strictEqual( isnan( v ), true, 'returns expected value' );
75119

76120
v = cabs( new Complex128( 5.0, NaN ) );
77-
t.strictEqual( isnan( v ), true, 'returns NaN' );
121+
t.strictEqual( isnan( v ), true, 'returns expected value' );
78122

79123
v = cabs( new Complex128( NaN, NaN ) );
80-
t.strictEqual( isnan( v ), true, 'returns NaN' );
124+
t.strictEqual( isnan( v ), true, 'returns expected value' );
81125

82126
t.end();
83127
});

Diff for: lib/node_modules/@stdlib/math/base/special/cabs/test/test.native.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var PINF = require( '@stdlib/constants/float64/pinf' );
28+
var NINF = require( '@stdlib/constants/float64/ninf' );
2729
var abs = require( '@stdlib/math/base/special/abs' );
2830
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2931
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -76,17 +78,59 @@ tape( 'the function computes the absolute value of a complex number', opts, func
7678
t.end();
7779
});
7880

79-
tape( 'if either the real or imaginary component is `NaN`, the function returns `NaN`', opts, function test( t ) {
81+
tape( 'if either the real or imaginary component is `+infinity`, the function returns `+infinity`', opts, function test( t ) {
82+
var v;
83+
84+
v = cabs( new Complex128( PINF, 3.0 ) );
85+
t.strictEqual( v, PINF, 'returns expected value' );
86+
87+
v = cabs( new Complex128( 5.0, PINF ) );
88+
t.strictEqual( v, PINF, 'returns expected value' );
89+
90+
v = cabs( new Complex128( PINF, PINF ) );
91+
t.strictEqual( v, PINF, 'returns expected value' );
92+
93+
v = cabs( new Complex128( NaN, PINF ) );
94+
t.strictEqual( v, PINF, 'returns expected value' );
95+
96+
v = cabs( new Complex128( PINF, NaN ) );
97+
t.strictEqual( v, PINF, 'returns expected value' );
98+
99+
t.end();
100+
});
101+
102+
tape( 'if either the real or imaginary component is `-infinity`, the function returns `+infinity`', opts, function test( t ) {
103+
var v;
104+
105+
v = cabs( new Complex128( NINF, 3.0 ) );
106+
t.strictEqual( v, PINF, 'returns expected value' );
107+
108+
v = cabs( new Complex128( 5.0, NINF ) );
109+
t.strictEqual( v, PINF, 'returns expected value' );
110+
111+
v = cabs( new Complex128( NINF, NINF ) );
112+
t.strictEqual( v, PINF, 'returns expected value' );
113+
114+
v = cabs( new Complex128( NaN, NINF ) );
115+
t.strictEqual( v, PINF, 'returns expected value' );
116+
117+
v = cabs( new Complex128( NINF, NaN ) );
118+
t.strictEqual( v, PINF, 'returns expected value' );
119+
120+
t.end();
121+
});
122+
123+
tape( 'if either the real or imaginary component is `NaN` but not `+-infinity`, the function returns `NaN`', opts, function test( t ) {
80124
var v;
81125

82126
v = cabs( new Complex128( NaN, 3.0 ) );
83-
t.strictEqual( isnan( v ), true, 'returns NaN' );
127+
t.strictEqual( isnan( v ), true, 'returns expected value' );
84128

85129
v = cabs( new Complex128( 5.0, NaN ) );
86-
t.strictEqual( isnan( v ), true, 'returns NaN' );
130+
t.strictEqual( isnan( v ), true, 'returns expected value' );
87131

88132
v = cabs( new Complex128( NaN, NaN ) );
89-
t.strictEqual( isnan( v ), true, 'returns NaN' );
133+
t.strictEqual( isnan( v ), true, 'returns expected value' );
90134

91135
t.end();
92136
});

0 commit comments

Comments
 (0)