Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var imag = require( '@stdlib/complex/float64/imag' );
* // returns ~5.83
*/
function cabs( z ) {
// 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)
return hypot( real( z ), imag( z ) );
}

Expand Down
52 changes: 48 additions & 4 deletions lib/node_modules/@stdlib/math/base/special/cabs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
var tape = require( 'tape' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var abs = require( '@stdlib/math/base/special/abs' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var cabs = require( './../lib' );
Expand Down Expand Up @@ -67,17 +69,59 @@ tape( 'the function computes the absolute value of a complex number', function t
t.end();
});

tape( 'if either the real or imaginary component is `NaN`, the function returns `NaN`', function test( t ) {
tape( 'if either the real or imaginary component is `+infinity`, the function returns `+infinity`', function test( t ) {
var v;

v = cabs( new Complex128( PINF, 3.0 ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( 5.0, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( PINF, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NaN, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( PINF, NaN ) );
t.strictEqual( v, PINF, 'returns expected value' );

t.end();
});

tape( 'if either the real or imaginary component is `-infinity`, the function returns `+infinity`', function test( t ) {
var v;

v = cabs( new Complex128( NINF, 3.0 ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( 5.0, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NINF, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NaN, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NINF, NaN ) );
t.strictEqual( v, PINF, 'returns expected value' );

t.end();
});

tape( 'if either the real or imaginary component is `NaN` but not `+-infinity`, the function returns `NaN`', function test( t ) {
var v;

v = cabs( new Complex128( NaN, 3.0 ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = cabs( new Complex128( 5.0, NaN ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = cabs( new Complex128( NaN, NaN ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var resolve = require( 'path' ).resolve;
var tape = require( 'tape' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var abs = require( '@stdlib/math/base/special/abs' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var tryRequire = require( '@stdlib/utils/try-require' );
Expand Down Expand Up @@ -76,17 +78,59 @@ tape( 'the function computes the absolute value of a complex number', opts, func
t.end();
});

tape( 'if either the real or imaginary component is `NaN`, the function returns `NaN`', opts, function test( t ) {
tape( 'if either the real or imaginary component is `+infinity`, the function returns `+infinity`', opts, function test( t ) {
var v;

v = cabs( new Complex128( PINF, 3.0 ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( 5.0, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( PINF, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NaN, PINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( PINF, NaN ) );
t.strictEqual( v, PINF, 'returns expected value' );

t.end();
});

tape( 'if either the real or imaginary component is `-infinity`, the function returns `+infinity`', opts, function test( t ) {
var v;

v = cabs( new Complex128( NINF, 3.0 ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( 5.0, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NINF, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NaN, NINF ) );
t.strictEqual( v, PINF, 'returns expected value' );

v = cabs( new Complex128( NINF, NaN ) );
t.strictEqual( v, PINF, 'returns expected value' );

t.end();
});

tape( 'if either the real or imaginary component is `NaN` but not `+-infinity`, the function returns `NaN`', opts, function test( t ) {
var v;

v = cabs( new Complex128( NaN, 3.0 ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = cabs( new Complex128( 5.0, NaN ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = cabs( new Complex128( NaN, NaN ) );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});