Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/cabs/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
// MODULES //

var hypot = require( '@stdlib/math/base/special/hypot' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

Expand All @@ -40,7 +43,12 @@ 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)
if ( isnan( real( z ) ) || isnan( imag( z ) ) ) {
return NaN;
}
if ( isInfinite( real( z ) ) || isInfinite( imag( z ) ) ) {
return PINF;
}
return hypot( real( z ), imag( z ) );
}

Expand Down
15 changes: 12 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/cabs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"@stdlib/math/base/napi/unary",
"@stdlib/complex/float64/ctor",
"@stdlib/complex/float64/reim",
"@stdlib/math/base/special/hypot"
"@stdlib/math/base/special/hypot",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/constants/float64/pinf"
]
},
{
Expand All @@ -55,7 +58,10 @@
"dependencies": [
"@stdlib/complex/float64/ctor",
"@stdlib/complex/float64/reim",
"@stdlib/math/base/special/hypot"
"@stdlib/math/base/special/hypot",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/constants/float64/pinf"
]
},
{
Expand All @@ -71,7 +77,10 @@
"dependencies": [
"@stdlib/complex/float64/ctor",
"@stdlib/complex/float64/reim",
"@stdlib/math/base/special/hypot"
"@stdlib/math/base/special/hypot",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/constants/float64/pinf"
]
}
]
Expand Down
10 changes: 10 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cabs/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include "stdlib/math/base/special/cabs.h"
#include "stdlib/math/base/special/hypot.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/assert/is_infinite.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/complex/float64/ctor.h"
#include "stdlib/complex/float64/reim.h"

Expand All @@ -39,5 +42,12 @@ double stdlib_base_cabs( const stdlib_complex128_t z ) {
double re;
double im;
stdlib_complex128_reim( z, &re, &im );

if ( stdlib_base_is_nan( re ) || stdlib_base_is_nan( im ) ) {
return 0.0 / 0.0; // NaN
}
if ( stdlib_base_is_infinite( re ) || stdlib_base_is_infinite( im ) ) {
return STDLIB_CONSTANT_FLOAT64_PINF;
}
return stdlib_base_hypot( re, im );
}
38 changes: 35 additions & 3 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 @@ -71,13 +73,43 @@ tape( 'if either the real or imaginary component is `NaN`, the function returns
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();
});

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' );

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' );

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 @@ -80,13 +82,43 @@ tape( 'if either the real or imaginary component is `NaN`, the function returns
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();
});

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' );

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' );

t.end();
});