Skip to content

Commit f0b5a73

Browse files
authored
refactor: replace built-in with stdlib_base_atan2 in math/base/special/cphase
PR-URL: #3231 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 16d48af commit f0b5a73

File tree

6 files changed

+118
-109
lines changed

6 files changed

+118
-109
lines changed

lib/node_modules/@stdlib/math/base/special/cphase/benchmark/c/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,20 @@ static double rand_double( void ) {
9090
*/
9191
static double benchmark( void ) {
9292
double elapsed;
93-
double re;
94-
double im;
93+
double re[ 100 ];
94+
double im[ 100 ];
9595
double y;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
re[ i ] = ( 1000.0 * rand_double() ) - 500.0;
101+
im[ i ] = ( 1000.0 * rand_double() ) - 500.0;
102+
}
103+
99104
t = tic();
100105
for ( i = 0; i < ITERATIONS; i++ ) {
101-
re = ( 1000.0*rand_double() ) - 500.0;
102-
im = ( 1000.0*rand_double() ) - 500.0;
103-
double complex z = re + im*I;
106+
double complex z = re[ i % 100 ] + im[ i % 100 ] * I;
104107
y = carg( z );
105108
if ( y != y ) {
106109
printf( "should not return NaN\n" );

lib/node_modules/@stdlib/math/base/special/cphase/benchmark/c/native/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,22 @@ static double rand_double( void ) {
9393
*/
9494
static double benchmark( void ) {
9595
double elapsed;
96-
double re;
97-
double im;
96+
double re[ 100 ];
97+
double im[ 100 ];
9898
double y;
9999
double t;
100100
int i;
101101

102102
stdlib_complex128_t z;
103103

104+
for ( i = 0; i < 100; i++ ) {
105+
re[ i ] = ( 1000.0 * rand_double() ) - 500.0;
106+
im[ i ] = ( 1000.0 * rand_double() ) - 500.0;
107+
}
108+
104109
t = tic();
105110
for ( i = 0; i < ITERATIONS; i++ ) {
106-
re = ( 1000.0*rand_double() ) - 500.0;
107-
im = ( 1000.0*rand_double() ) - 500.0;
108-
z = stdlib_complex128( re, im );
111+
z = stdlib_complex128( re[ i % 100 ], im[ i % 100 ] );
109112
y = stdlib_base_cphase( z );
110113
if ( y != y ) {
111114
printf( "should not return NaN\n" );

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
4040
"@stdlib/complex/float64/ctor",
41-
"@stdlib/complex/float64/reim"
41+
"@stdlib/complex/float64/reim",
42+
"@stdlib/math/base/special/atan2"
4243
]
4344
},
4445
{
@@ -53,7 +54,8 @@
5354
"libpath": [],
5455
"dependencies": [
5556
"@stdlib/complex/float64/ctor",
56-
"@stdlib/complex/float64/reim"
57+
"@stdlib/complex/float64/reim",
58+
"@stdlib/math/base/special/atan2"
5759
]
5860
},
5961
{
@@ -68,7 +70,8 @@
6870
"libpath": [],
6971
"dependencies": [
7072
"@stdlib/complex/float64/ctor",
71-
"@stdlib/complex/float64/reim"
73+
"@stdlib/complex/float64/reim",
74+
"@stdlib/math/base/special/atan2"
7275
]
7376
}
7477
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "stdlib/math/base/special/cphase.h"
2020
#include "stdlib/complex/float64/ctor.h"
2121
#include "stdlib/complex/float64/reim.h"
22-
#include <math.h>
22+
#include "stdlib/math/base/special/atan2.h"
2323

2424
/**
2525
* Computes the argument of a complex double-precision complex floating-point number in radians.
@@ -41,5 +41,5 @@ double stdlib_base_cphase( const stdlib_complex128_t z ) {
4141
double im;
4242

4343
stdlib_complex128_reim( z, &re, &im );
44-
return atan2( im, re ); // TODO: replace with stdlib function once available
44+
return stdlib_base_atan2( im, re );
4545
}

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

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,113 +48,113 @@ tape( 'main export is a function', function test( t ) {
4848
});
4949

5050
tape( 'the function returns `NaN` if provided `NaN` as either of the arguments', function test( t ) {
51-
t.strictEqual( isnan( cphase( new Complex128( 2.0, NaN ) ) ), true, 'returns NaN' );
52-
t.strictEqual( isnan( cphase( new Complex128( NaN, 3.0 ) ) ), true, 'returns NaN' );
51+
t.strictEqual( isnan( cphase( new Complex128( 2.0, NaN ) ) ), true, 'returns expected value' );
52+
t.strictEqual( isnan( cphase( new Complex128( NaN, 3.0 ) ) ), true, 'returns expected value' );
5353
t.end();
5454
});
5555

5656
tape( 'the function returns `+0` if provided `im = +0.0` and `re >= 0`', function test( t ) {
57-
t.strictEqual( cphase( new Complex128( 0.0, +0.0) ), +0.0, 'returns +0' );
58-
t.strictEqual( cphase( new Complex128( 2.0, +0.0) ), +0.0, 'returns +0' );
59-
t.strictEqual( cphase( new Complex128( 4.0, +0.0) ), +0.0, 'returns +0' );
60-
t.strictEqual( cphase( new Complex128( 5.0, +0.0) ), +0.0, 'returns +0' );
61-
t.strictEqual( cphase( new Complex128( 10.0, +0.0) ), +0.0, 'returns +0' );
57+
t.strictEqual( cphase( new Complex128( 0.0, +0.0) ), +0.0, 'returns expected value' );
58+
t.strictEqual( cphase( new Complex128( 2.0, +0.0) ), +0.0, 'returns expected value' );
59+
t.strictEqual( cphase( new Complex128( 4.0, +0.0) ), +0.0, 'returns expected value' );
60+
t.strictEqual( cphase( new Complex128( 5.0, +0.0) ), +0.0, 'returns expected value' );
61+
t.strictEqual( cphase( new Complex128( 10.0, +0.0) ), +0.0, 'returns expected value' );
6262
t.end();
6363
});
6464

6565
tape( 'the function returns `-0` if provided `im = -0.0` and `re >= 0`', function test( t ) {
66-
t.strictEqual( isNegativeZero( cphase( new Complex128( 0.0, -0.0 ) ) ), true, 'returns -0' );
67-
t.strictEqual( isNegativeZero( cphase( new Complex128( 2.0, -0.0 ) ) ), true, 'returns -0' );
68-
t.strictEqual( isNegativeZero( cphase( new Complex128( 4.0, -0.0 ) ) ), true, 'returns -0' );
69-
t.strictEqual( isNegativeZero( cphase( new Complex128( 5.0, -0.0 ) ) ), true, 'returns -0' );
70-
t.strictEqual( isNegativeZero( cphase( new Complex128( 10.0, -0.0 ) ) ), true, 'returns -0' );
66+
t.strictEqual( isNegativeZero( cphase( new Complex128( 0.0, -0.0 ) ) ), true, 'returns expected value' );
67+
t.strictEqual( isNegativeZero( cphase( new Complex128( 2.0, -0.0 ) ) ), true, 'returns expected value' );
68+
t.strictEqual( isNegativeZero( cphase( new Complex128( 4.0, -0.0 ) ) ), true, 'returns expected value' );
69+
t.strictEqual( isNegativeZero( cphase( new Complex128( 5.0, -0.0 ) ) ), true, 'returns expected value' );
70+
t.strictEqual( isNegativeZero( cphase( new Complex128( 10.0, -0.0 ) ) ), true, 'returns expected value' );
7171
t.end();
7272
});
7373

7474
tape( 'the function returns `PI` if provided `im = +0.0` and `re <= -0.0`', function test( t ) {
75-
t.strictEqual( cphase( new Complex128( -0.0, +0.0 ) ), +PI, 'returns +PI' );
76-
t.strictEqual( cphase( new Complex128( -2.0, +0.0 ) ), +PI, 'returns +PI' );
77-
t.strictEqual( cphase( new Complex128( -4.0, +0.0 ) ), +PI, 'returns +PI' );
78-
t.strictEqual( cphase( new Complex128( -5.0, +0.0 ) ), +PI, 'returns +PI' );
79-
t.strictEqual( cphase( new Complex128( -10.0, +0.0 ) ), +PI, 'returns +PI' );
75+
t.strictEqual( cphase( new Complex128( -0.0, +0.0 ) ), +PI, 'returns expected value' );
76+
t.strictEqual( cphase( new Complex128( -2.0, +0.0 ) ), +PI, 'returns expected value' );
77+
t.strictEqual( cphase( new Complex128( -4.0, +0.0 ) ), +PI, 'returns expected value' );
78+
t.strictEqual( cphase( new Complex128( -5.0, +0.0 ) ), +PI, 'returns expected value' );
79+
t.strictEqual( cphase( new Complex128( -10.0, +0.0 ) ), +PI, 'returns expected value' );
8080
t.end();
8181
});
8282

8383
tape( 'the function returns `-PI` if provided `im = -0.0` and `re <= -0.0`', function test( t ) {
84-
t.strictEqual( cphase( new Complex128( -0.0, -0.0 ) ), -PI, 'returns -PI' );
85-
t.strictEqual( cphase( new Complex128( -2.0, -0.0 ) ), -PI, 'returns -PI' );
86-
t.strictEqual( cphase( new Complex128( -4.0, -0.0 ) ), -PI, 'returns -PI' );
87-
t.strictEqual( cphase( new Complex128( -5.0, -0.0 ) ), -PI, 'returns -PI' );
88-
t.strictEqual( cphase( new Complex128( -10.0, -0.0 ) ), -PI, 'returns -PI' );
84+
t.strictEqual( cphase( new Complex128( -0.0, -0.0 ) ), -PI, 'returns expected value' );
85+
t.strictEqual( cphase( new Complex128( -2.0, -0.0 ) ), -PI, 'returns expected value' );
86+
t.strictEqual( cphase( new Complex128( -4.0, -0.0 ) ), -PI, 'returns expected value' );
87+
t.strictEqual( cphase( new Complex128( -5.0, -0.0 ) ), -PI, 'returns expected value' );
88+
t.strictEqual( cphase( new Complex128( -10.0, -0.0 ) ), -PI, 'returns expected value' );
8989
t.end();
9090
});
9191

9292
tape( 'the function returns `+PI/4` if provided `re = im = +infinity`', function test( t ) {
93-
t.strictEqual( cphase( new Complex128( PINF, PINF ) ), +PI/4.0, 'returns +PI/4' );
93+
t.strictEqual( cphase( new Complex128( PINF, PINF ) ), +PI/4.0, 'returns expected value' );
9494
t.end();
9595
});
9696

9797
tape( 'the function returns `-PI/4` if provided `re = -im = +infinity`', function test( t ) {
98-
t.strictEqual( cphase( new Complex128( PINF, NINF ) ), -PI/4.0, 'returns -PI/4' );
98+
t.strictEqual( cphase( new Complex128( PINF, NINF ) ), -PI/4.0, 'returns expected value' );
9999
t.end();
100100
});
101101

102102
tape( 'the function returns `*3*PI/4` if provided `-re = im = +infinity`', function test( t ) {
103-
t.strictEqual( cphase( new Complex128( NINF, PINF ) ), +3.0*PI/4.0, 'returns +3*PI/4' );
103+
t.strictEqual( cphase( new Complex128( NINF, PINF ) ), +3.0*PI/4.0, 'returns expected value' );
104104
t.end();
105105
});
106106

107107
tape( 'the function returns `-3*PI/4` if provided `re = im = -infinity`', function test( t ) {
108-
t.strictEqual( cphase( new Complex128( NINF, NINF ) ), -3.0*PI/4.0, 'returns -3*PI/4' );
108+
t.strictEqual( cphase( new Complex128( NINF, NINF ) ), -3.0*PI/4.0, 'returns expected value' );
109109
t.end();
110110
});
111111

112112
tape( 'the function returns `0.0` when `re = +infinity`', function test( t ) {
113-
t.strictEqual( cphase( new Complex128( PINF, -2.0 ) ), 0.0, 'returns 0.0' );
114-
t.strictEqual( cphase( new Complex128( PINF, 0.0 ) ), 0.0, 'returns 0.0' );
115-
t.strictEqual( cphase( new Complex128( PINF, 2.0 ) ), 0.0, 'returns 0.0' );
113+
t.strictEqual( cphase( new Complex128( PINF, -2.0 ) ), 0.0, 'returns expected value' );
114+
t.strictEqual( cphase( new Complex128( PINF, 0.0 ) ), 0.0, 'returns expected value' );
115+
t.strictEqual( cphase( new Complex128( PINF, 2.0 ) ), 0.0, 'returns expected value' );
116116
t.end();
117117
});
118118

119119
tape( 'the function returns `+PI` when `im > 0` and `re = -infinity`', function test( t ) {
120-
t.strictEqual( cphase( new Complex128( NINF, 1.0 ) ), PI, 'returns PI' );
121-
t.strictEqual( cphase( new Complex128( NINF, 2.0 ) ), PI, 'returns PI' );
122-
t.strictEqual( cphase( new Complex128( NINF, 3.0 ) ), PI, 'returns PI' );
120+
t.strictEqual( cphase( new Complex128( NINF, 1.0 ) ), PI, 'returns expected value' );
121+
t.strictEqual( cphase( new Complex128( NINF, 2.0 ) ), PI, 'returns expected value' );
122+
t.strictEqual( cphase( new Complex128( NINF, 3.0 ) ), PI, 'returns expected value' );
123123
t.end();
124124
});
125125

126126
tape( 'the function returns `-PI` when `im < 0` and `re = -infinity`', function test( t ) {
127-
t.strictEqual( cphase( new Complex128( NINF, -1.0 ) ), -PI, 'returns -PI' );
128-
t.strictEqual( cphase( new Complex128( NINF, -2.0 ) ), -PI, 'returns -PI' );
129-
t.strictEqual( cphase( new Complex128( NINF, -3.0 ) ), -PI, 'returns -PI' );
127+
t.strictEqual( cphase( new Complex128( NINF, -1.0 ) ), -PI, 'returns expected value' );
128+
t.strictEqual( cphase( new Complex128( NINF, -2.0 ) ), -PI, 'returns expected value' );
129+
t.strictEqual( cphase( new Complex128( NINF, -3.0 ) ), -PI, 'returns expected value' );
130130
t.end();
131131
});
132132

133133
tape( 'the function returns `+PI/2` when `im = +infinity`', function test( t ) {
134-
t.strictEqual( cphase( new Complex128( -1.0, PINF ) ), PI/2.0, 'returns PI/2' );
135-
t.strictEqual( cphase( new Complex128( 0.0, PINF ) ), PI/2.0, 'returns PI/2' );
136-
t.strictEqual( cphase( new Complex128( 2.0, PINF ) ), PI/2.0, 'returns PI/2' );
134+
t.strictEqual( cphase( new Complex128( -1.0, PINF ) ), PI/2.0, 'returns expected value' );
135+
t.strictEqual( cphase( new Complex128( 0.0, PINF ) ), PI/2.0, 'returns expected value' );
136+
t.strictEqual( cphase( new Complex128( 2.0, PINF ) ), PI/2.0, 'returns expected value' );
137137
t.end();
138138
});
139139

140140
tape( 'the function returns `-PI/2` when `im = -infinity`', function test( t ) {
141-
t.strictEqual( cphase( new Complex128( -1.0, NINF ) ), -PI/2.0, 'returns -PI/2' );
142-
t.strictEqual( cphase( new Complex128( 0.0, NINF ) ), -PI/2.0, 'returns -PI/2' );
143-
t.strictEqual( cphase( new Complex128( 2.0, NINF ) ), -PI/2.0, 'returns -PI/2' );
141+
t.strictEqual( cphase( new Complex128( -1.0, NINF ) ), -PI/2.0, 'returns expected value' );
142+
t.strictEqual( cphase( new Complex128( 0.0, NINF ) ), -PI/2.0, 'returns expected value' );
143+
t.strictEqual( cphase( new Complex128( 2.0, NINF ) ), -PI/2.0, 'returns expected value' );
144144
t.end();
145145
});
146146

147147
tape( 'the function returns `PI/2` if provided a positive `im` and `re=0`', function test( t ) {
148-
t.strictEqual( cphase( new Complex128( 0.0, 2.0 ) ), PI/2.0, 'returns PI/2' );
149-
t.strictEqual( cphase( new Complex128( 0.0, 1.0 ) ), PI/2.0, 'returns PI/2' );
150-
t.strictEqual( cphase( new Complex128( 0.0, 0.5 ) ), PI/2.0, 'returns PI/2' );
148+
t.strictEqual( cphase( new Complex128( 0.0, 2.0 ) ), PI/2.0, 'returns expected value' );
149+
t.strictEqual( cphase( new Complex128( 0.0, 1.0 ) ), PI/2.0, 'returns expected value' );
150+
t.strictEqual( cphase( new Complex128( 0.0, 0.5 ) ), PI/2.0, 'returns expected value' );
151151
t.end();
152152
});
153153

154154
tape( 'the function returns `-PI/2` if provided a negative `im` and `re=0`', function test( t ) {
155-
t.strictEqual( cphase( new Complex128( 0.0, -2.0 ) ), -PI/2.0, 'returns PI/2' );
156-
t.strictEqual( cphase( new Complex128( 0.0, -1.0 ) ), -PI/2.0, 'returns PI/2' );
157-
t.strictEqual( cphase( new Complex128( 0.0, -0.5 ) ), -PI/2.0, 'returns PI/2' );
155+
t.strictEqual( cphase( new Complex128( 0.0, -2.0 ) ), -PI/2.0, 'returns expected value' );
156+
t.strictEqual( cphase( new Complex128( 0.0, -1.0 ) ), -PI/2.0, 'returns expected value' );
157+
t.strictEqual( cphase( new Complex128( 0.0, -0.5 ) ), -PI/2.0, 'returns expected value' );
158158
t.end();
159159
});
160160

0 commit comments

Comments
 (0)