Skip to content

Commit daa14d9

Browse files
committed
chore: improve implementation and 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: passed - 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: passed - 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 a77fb9e commit daa14d9

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// MODULES //
2222

2323
var hypot = require( '@stdlib/math/base/special/hypot' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );
26+
var PINF = require( '@stdlib/constants/float64/pinf' );
2427
var real = require( '@stdlib/complex/float64/real' );
2528
var imag = require( '@stdlib/complex/float64/imag' );
2629

@@ -40,7 +43,12 @@ var imag = require( '@stdlib/complex/float64/imag' );
4043
* // returns ~5.83
4144
*/
4245
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)
46+
if ( isnan( real( z ) ) || isnan( imag( z ) ) ) {
47+
return NaN;
48+
}
49+
if ( isInfinite( real( z ) ) || isInfinite( imag( z ) ) ) {
50+
return PINF;
51+
}
4452
return hypot( real( z ), imag( z ) );
4553
}
4654

lib/node_modules/@stdlib/math/base/special/cabs/manifest.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/complex/float64/ctor",
4141
"@stdlib/complex/float64/reim",
42-
"@stdlib/math/base/special/hypot"
42+
"@stdlib/math/base/special/hypot",
43+
"@stdlib/math/base/assert/is-nan",
44+
"@stdlib/math/base/assert/is-infinite",
45+
"@stdlib/constants/float64/pinf"
4346
]
4447
},
4548
{
@@ -55,7 +58,10 @@
5558
"dependencies": [
5659
"@stdlib/complex/float64/ctor",
5760
"@stdlib/complex/float64/reim",
58-
"@stdlib/math/base/special/hypot"
61+
"@stdlib/math/base/special/hypot",
62+
"@stdlib/math/base/assert/is-nan",
63+
"@stdlib/math/base/assert/is-infinite",
64+
"@stdlib/constants/float64/pinf"
5965
]
6066
},
6167
{
@@ -71,7 +77,10 @@
7177
"dependencies": [
7278
"@stdlib/complex/float64/ctor",
7379
"@stdlib/complex/float64/reim",
74-
"@stdlib/math/base/special/hypot"
80+
"@stdlib/math/base/special/hypot",
81+
"@stdlib/math/base/assert/is-nan",
82+
"@stdlib/math/base/assert/is-infinite",
83+
"@stdlib/constants/float64/pinf"
7584
]
7685
}
7786
]

lib/node_modules/@stdlib/math/base/special/cabs/src/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include "stdlib/math/base/special/cabs.h"
2020
#include "stdlib/math/base/special/hypot.h"
21+
#include "stdlib/math/base/assert/is_nan.h"
22+
#include "stdlib/math/base/assert/is_infinite.h"
23+
#include "stdlib/constants/float64/pinf.h"
2124
#include "stdlib/complex/float64/ctor.h"
2225
#include "stdlib/complex/float64/reim.h"
2326

@@ -39,5 +42,12 @@ double stdlib_base_cabs( const stdlib_complex128_t z ) {
3942
double re;
4043
double im;
4144
stdlib_complex128_reim( z, &re, &im );
45+
46+
if ( stdlib_base_is_nan( re ) || stdlib_base_is_nan( im ) ) {
47+
return 0.0 / 0.0; // NaN
48+
}
49+
if ( stdlib_base_is_infinite( re ) || stdlib_base_is_infinite( im ) ) {
50+
return STDLIB_CONSTANT_FLOAT64_PINF;
51+
}
4252
return stdlib_base_hypot( re, im );
4353
}

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

Lines changed: 35 additions & 3 deletions
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' );
@@ -71,13 +73,43 @@ tape( 'if either the real or imaginary component is `NaN`, the function returns
7173
var v;
7274

7375
v = cabs( new Complex128( NaN, 3.0 ) );
74-
t.strictEqual( isnan( v ), true, 'returns NaN' );
76+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7577

7678
v = cabs( new Complex128( 5.0, NaN ) );
77-
t.strictEqual( isnan( v ), true, 'returns NaN' );
79+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7880

7981
v = cabs( new Complex128( NaN, NaN ) );
80-
t.strictEqual( isnan( v ), true, 'returns NaN' );
82+
t.strictEqual( isnan( v ), true, 'returns expected value' );
83+
84+
t.end();
85+
});
86+
87+
tape( 'if either the real or imaginary component is `+Infinity`, the function returns `+Infinity`', function test( t ) {
88+
var v;
89+
90+
v = cabs( new Complex128( PINF, 3.0 ) );
91+
t.strictEqual( v, PINF, 'returns expected value' );
92+
93+
v = cabs( new Complex128( 5.0, PINF ) );
94+
t.strictEqual( v, PINF, 'returns expected value' );
95+
96+
v = cabs( new Complex128( PINF, PINF ) );
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`', 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' );
81113

82114
t.end();
83115
});

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

Lines changed: 35 additions & 3 deletions
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' );
@@ -80,13 +82,43 @@ tape( 'if either the real or imaginary component is `NaN`, the function returns
8082
var v;
8183

8284
v = cabs( new Complex128( NaN, 3.0 ) );
83-
t.strictEqual( isnan( v ), true, 'returns NaN' );
85+
t.strictEqual( isnan( v ), true, 'returns expected value' );
8486

8587
v = cabs( new Complex128( 5.0, NaN ) );
86-
t.strictEqual( isnan( v ), true, 'returns NaN' );
88+
t.strictEqual( isnan( v ), true, 'returns expected value' );
8789

8890
v = cabs( new Complex128( NaN, NaN ) );
89-
t.strictEqual( isnan( v ), true, 'returns NaN' );
91+
t.strictEqual( isnan( v ), true, 'returns expected value' );
92+
93+
t.end();
94+
});
95+
96+
tape( 'if either the real or imaginary component is `+Infinity`, the function returns `+Infinity`', opts, function test( t ) {
97+
var v;
98+
99+
v = cabs( new Complex128( PINF, 3.0 ) );
100+
t.strictEqual( v, PINF, 'returns expected value' );
101+
102+
v = cabs( new Complex128( 5.0, PINF ) );
103+
t.strictEqual( v, PINF, 'returns expected value' );
104+
105+
v = cabs( new Complex128( PINF, PINF ) );
106+
t.strictEqual( v, PINF, 'returns expected value' );
107+
108+
t.end();
109+
});
110+
111+
tape( 'if either the real or imaginary component is `-Infinity`, the function returns `+Infinity`', opts, function test( t ) {
112+
var v;
113+
114+
v = cabs( new Complex128( NINF, 3.0 ) );
115+
t.strictEqual( v, PINF, 'returns expected value' );
116+
117+
v = cabs( new Complex128( 5.0, NINF ) );
118+
t.strictEqual( v, PINF, 'returns expected value' );
119+
120+
v = cabs( new Complex128( NINF, NINF ) );
121+
t.strictEqual( v, PINF, 'returns expected value' );
90122

91123
t.end();
92124
});

0 commit comments

Comments
 (0)