Skip to content

Commit f9cd43c

Browse files
anandkaranubckgrytestdlib-bot
authored
feat: add math/base/special/atan2d
PR-URL: #6273 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 1aa6c6d commit f9cd43c

31 files changed

+2428
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# atan2d
22+
23+
> Compute the angle in the plane (in degrees) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var atan2d = require( '@stdlib/math/base/special/atan2d' );
31+
```
32+
33+
#### atan2d( y, x )
34+
35+
Computes the angle in the plane (in degrees) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
36+
37+
```javascript
38+
var v = atan2d( 2.0, 2.0 ); // => atand(1.0)
39+
// returns ~45.0
40+
41+
v = atan2d( 6.0, 2.0 ); // => atand(3.0)
42+
// returns ~71.565
43+
44+
v = atan2d( -1.0, -1.0 ); // => atand(1.0) - 180.0
45+
// returns ~-135.0
46+
47+
v = atan2d( 3.0, 0.0 );
48+
// returns 90.0
49+
50+
v = atan2d( -2.0, 0.0 );
51+
// returns -90.0
52+
53+
v = atan2d( 0.0, 0.0 );
54+
// returns 0.0
55+
56+
v = atan2d( 3.0, NaN );
57+
// returns NaN
58+
```
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="examples">
65+
66+
## Examples
67+
68+
<!-- eslint no-undef: "error" -->
69+
70+
```javascript
71+
var uniform = require( '@stdlib/random/array/uniform' );
72+
var logEachMap = require( '@stdlib/console/log-each-map' );
73+
var atan2d = require( '@stdlib/math/base/special/atan2d' );
74+
75+
var x = uniform( 100, 0.0, 100.0 );
76+
var y = uniform( 100, 0.0, 100.0 );
77+
78+
logEachMap( 'y: %0.4f, x: %0.4f, atan2d(y,x): %0.4f', y, x, atan2d );
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
#include "stdlib/math/base/special/atan2d.h"
109+
```
110+
111+
#### stdlib_base_atan2d( y, x )
112+
113+
Computes the angle in the plane (in degrees) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
114+
115+
```c
116+
double out = stdlib_base_atan2d( 2.0, 2.0 );
117+
// returns ~45.0
118+
119+
out = stdlib_base_atan2d( 6.0, 2.0 );
120+
// returns ~71.565
121+
```
122+
123+
The function accepts the following arguments:
124+
125+
- **y**: `[in] double` - `y` coordinate.
126+
- **x**: `[in] double` - `x` coordinate.
127+
128+
```c
129+
double stdlib_base_atan2d( const double y, const double x );
130+
```
131+
132+
</section>
133+
134+
<!-- /.usage -->
135+
136+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="notes">
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<!-- C API usage examples. -->
145+
146+
<section class="examples">
147+
148+
### Examples
149+
150+
```c
151+
#include "stdlib/math/base/special/atan2d.h"
152+
#include <stdlib.h>
153+
#include <stdio.h>
154+
155+
static double random_uniform( const double min, const double max ) {
156+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
157+
return min + ( v*(max-min) );
158+
}
159+
160+
int main( void ) {
161+
double y;
162+
double x;
163+
double v;
164+
int i;
165+
166+
for ( i = 0; i < 100; i++ ) {
167+
y = random_uniform( 0.0, 100.0 );
168+
x = random_uniform( 0.0, 100.0 );
169+
v = stdlib_base_atan2d( y, x );
170+
printf( "atan2d(%lf, %lf) = %lf\n", y, x, v );
171+
}
172+
}
173+
```
174+
175+
</section>
176+
177+
<!-- /.examples -->
178+
179+
</section>
180+
181+
<!-- /.c -->
182+
183+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
184+
185+
<section class="related">
186+
187+
</section>
188+
189+
<!-- /.related -->
190+
191+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
192+
193+
<section class="links">
194+
195+
<!-- <related-links> -->
196+
197+
<!-- </related-links> -->
198+
199+
</section>
200+
201+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var atan2d = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var z;
36+
var i;
37+
38+
x = uniform( 100, 0.0, 100.0 );
39+
y = uniform( 100, 0.0, 100.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
z = atan2d( x[ i%x.length ], y[ i%y.length ] );
44+
if ( isnan( z ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( z ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var atan2d = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( atan2d instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var z;
45+
var i;
46+
47+
x = uniform( 100, 0.0, 100.0 );
48+
y = uniform( 100, 0.0, 100.0 );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
z = atan2d( x[ i%x.length ], y[ i%y.length ] );
53+
if ( isnan( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( z ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)