Skip to content

Commit 09ae59f

Browse files
committed
feat: add C implementation for atan2
1 parent 2d26f29 commit 09ae59f

File tree

15 files changed

+1437
-0
lines changed

15 files changed

+1437
-0
lines changed

lib/node_modules/@stdlib/math/base/special/atan2/README.md

100644100755
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,98 @@ for ( i = 0; i < 100; i++ ) {
8686

8787
<!-- /.examples -->
8888

89+
90+
<!-- C interface documentation. -->
91+
92+
* * *
93+
94+
<section class="c">
95+
96+
## C APIs
97+
98+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
99+
100+
<section class="intro">
101+
102+
</section>
103+
104+
<!-- /.intro -->
105+
106+
<!-- C usage documentation. -->
107+
108+
<section class="usage">
109+
110+
### Usage
111+
112+
```c
113+
#include "stdlib/math/base/special/atan2.h"
114+
```
115+
116+
#### stdlib_base_atan2( x )
117+
118+
Compute the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
119+
120+
```c
121+
double out = stdlib_base_atan2( 2.0, 2.0 );
122+
// returns ~0.785
123+
124+
out = stdlib_base_atan2( 6.0, 2.0 );
125+
// returns ~1.249
126+
```
127+
128+
The function accepts the following arguments:
129+
130+
- **x**: `[in] double` input value.
131+
132+
```c
133+
double stdlib_base_atan2( const double x );
134+
```
135+
136+
</section>
137+
138+
<!-- /.usage -->
139+
140+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="notes">
143+
144+
</section>
145+
146+
<!-- /.notes -->
147+
148+
<!-- C API usage examples. -->
149+
150+
<section class="examples">
151+
152+
### Examples
153+
154+
```c
155+
#include "stdlib/math/base/special/atan2.h"
156+
#include "stdlib/random/base/randu"
157+
#include <stdio.h>
158+
159+
int main( void ) {
160+
double y;
161+
double x;
162+
int i;
163+
164+
for ( i = 0; i < 100; i++ ) {
165+
y = stdlib_base_random_randu(100);
166+
x = stdlib_base_random_randu(100);
167+
printf("y: %.4f, \t x: %.4f, \t atan2(y,x): %.4f\n", y, x, stdlib_base_atan2(y, x));
168+
}
169+
return 0;
170+
}
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
</section>
178+
179+
<!-- /.c -->
180+
89181
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
90182

91183
<section class="related">
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 randu = require( '@stdlib/random/base/randu' );
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 atan2 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( atan2 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+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
x = ( randu()*100.0 ) - 0.0;
50+
y = ( randu()*100.0 ) - 0.0;
51+
z = Math.atan2( x, y ); // eslint-disable-line stdlib/no-builtin-math
52+
if ( isnan( z ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( z ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2018 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+
20+
# VARIABLES #
21+
22+
ifndef VERBOSE
23+
QUIET := @
24+
endif
25+
26+
# Determine the OS:
27+
#
28+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
29+
# [2]: http://stackoverflow.com/a/27776822/2225624
30+
OS ?= $(shell uname)
31+
ifneq (, $(findstring MINGW,$(OS)))
32+
OS := WINNT
33+
else
34+
ifneq (, $(findstring MSYS,$(OS)))
35+
OS := WINNT
36+
else
37+
ifneq (, $(findstring CYGWIN,$(OS)))
38+
OS := WINNT
39+
endif
40+
endif
41+
endif
42+
43+
# Define the program used for compiling C source files:
44+
ifdef C_COMPILER
45+
CC := $(C_COMPILER)
46+
else
47+
CC := gcc
48+
endif
49+
50+
# Define the command-line options when compiling C files:
51+
CFLAGS ?= \
52+
-std=c99 \
53+
-O3 \
54+
-Wall \
55+
-pedantic
56+
57+
# Determine whether to generate [position independent code][1]:
58+
#
59+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
60+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
61+
ifeq ($(OS), WINNT)
62+
fPIC ?=
63+
else
64+
fPIC ?= -fPIC
65+
endif
66+
67+
# List of C targets:
68+
c_targets := benchmark.out
69+
70+
71+
# TARGETS #
72+
73+
# Default target.
74+
#
75+
# This target is the default target.
76+
77+
all: $(c_targets)
78+
79+
.PHONY: all
80+
81+
82+
# Compile C source.
83+
#
84+
# This target compiles C source files.
85+
86+
$(c_targets): %.out: %.c
87+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
88+
89+
90+
# Run a benchmark.
91+
#
92+
# This target runs a benchmark.
93+
94+
run: $(c_targets)
95+
$(QUIET) ./$<
96+
97+
.PHONY: run
98+
99+
100+
# Perform clean-up.
101+
#
102+
# This target removes generated files.
103+
104+
clean:
105+
$(QUIET) -rm -f *.o *.out
106+
107+
.PHONY: clean

0 commit comments

Comments
 (0)