Skip to content

Commit 2960d3a

Browse files
feat: basic c files added
1 parent a23c289 commit 2960d3a

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
#ifndef STDLIB_MATH_BASE_SPECIAL_ATAN2F_H
20+
#define STDLIB_MATH_BASE_SPECIAL_ATAN2F_H
21+
22+
/*
23+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
24+
*/
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/**
30+
* Computes the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
31+
*/
32+
float stdlib_base_atan2f( const float y, const float x );
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif // !STDLIB_MATH_BASE_SPECIAL_ATAN2F_H
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
49+
# RULES #
50+
51+
#/
52+
# Removes generated files for building an add-on.
53+
#
54+
# @example
55+
# make clean-addon
56+
#/
57+
clean-addon:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean-addon
61+
62+
#/
63+
# Removes generated files.
64+
#
65+
# @example
66+
# make clean
67+
#/
68+
clean: clean-addon
69+
70+
.PHONY: clean
71+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
#include "stdlib/math/base/special/atan2f.h"
20+
#include "stdlib/math/base/napi/binary.h"
21+
22+
// cppcheck-suppress shadowFunction
23+
STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_atan2f )
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
* ## Notice
20+
*
21+
* The original C code, long comment, copyright, license, and constants are from [Cephes]{@link http://www.netlib.org/cephes}. The implementation follows the original, but has been modified according to project conventions.
22+
*
23+
* ```text
24+
* Copyright 1984, 1995, 2000 by Stephen L. Moshier
25+
*
26+
* Some software in this archive may be from the book _Methods and Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) or from the Cephes Mathematical Library, a commercial product. In either event, it is copyrighted by the author. What you see here may be used freely but it comes with no support or guarantee.
27+
*
28+
* Stephen L. Moshier
29+
30+
* ```
31+
*/
32+
33+
#include "stdlib/math/base/assert/is_infinite.h"
34+
#include "stdlib/math/base/special/copysign.h"
35+
#include "stdlib/number/float64/base/signbit.h"
36+
#include "stdlib/math/base/assert/is_nanf.h"
37+
#include "stdlib/math/base/special/atan2.h"
38+
#include "stdlib/constants/float64/pinf.h"
39+
#include "stdlib/constants/float64/pi.h"
40+
#include <stdint.h>
41+
42+
// MAIN //
43+
44+
/**
45+
* Computes the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
46+
*
47+
* @param y `y` coordinate
48+
* @param x `x` coordinate
49+
* @return angle (in radians)
50+
*
51+
* @example
52+
* float v = stdlib_base_atan2f( 2.0, 2.0 ); // => stdlib_base_atanf(1.0f)
53+
* // returns ~0.785f
54+
*
55+
* @example
56+
* float v = stdlib_base_atan2f( 6.0, 2.0 ); // => stdlib_base_atanf(3.0f)
57+
* // returns ~1.249f
58+
*
59+
* @example
60+
* float v = stdlib_base_atan2f( -1.0, -1.0f ); // => stdlib_base_atanf(1.0f) - π
61+
* // returns ~-2.356f
62+
*
63+
* @example
64+
* float v = stdlib_base_atan2f( 3.0f, 0.0f ); // => π/2
65+
* // returns ~1.571ff
66+
* @example
67+
* float v = stdlib_base_atan2f( -2.0f, 0.0f ); // => -π/2
68+
* // returns ~-1.571f
69+
*
70+
* @example
71+
* float v = stdlib_base_atan2f( 0.0f, 0.0f );
72+
* // returns 0.0f
73+
*
74+
* @example
75+
* float v = stdlib_base_atan2f( 3.0f, NaN );
76+
* // returns NaN
77+
*
78+
* @example
79+
* float v = stdlib_base_atan2f( NaN, 2.0f );
80+
* // returns NaN
81+
*/
82+
float stdlib_base_atan2f( const float y, const float x ) {
83+
float q;
84+
if ( stdlib_base_is_nanf( x ) || stdlib_base_is_nanf( y ) ) {
85+
return 0.0f / 0.0f; // float
86+
}
87+
if ( stdlib_base_is_infinite( x ) ) {
88+
if ( x == STDLIB_CONSTANT_FLOAT64_PINF ) {
89+
if ( stdlib_base_is_infinite( y ) ) {
90+
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI / 4.0f, y );
91+
}
92+
return stdlib_base_copysign( 0.0f, y );
93+
}
94+
// Case: x is -Infinity
95+
if ( stdlib_base_is_infinite( y ) ) {
96+
return stdlib_base_copysign( 3.0f * STDLIB_CONSTANT_FLOAT64_PI / 4.0f, y );
97+
}
98+
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI, y );
99+
}
100+
if ( stdlib_base_is_infinite( y ) ) {
101+
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI / 2.0f, y );
102+
}
103+
if ( y == 0.0f ) {
104+
if ( x >= 0.0f && !stdlib_base_float64_signbit( x ) ) {
105+
return stdlib_base_copysign( 0.0, y );
106+
}
107+
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI, y );
108+
}
109+
if ( x == 0.0f ) {
110+
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI / 2.0f, y );
111+
}
112+
q = stdlib_base_atan2( y / x );
113+
if ( x < 0.0f ) {
114+
if ( q <= 0.0f ) {
115+
return q + STDLIB_CONSTANT_FLOAT64_PI;
116+
}
117+
return q - STDLIB_CONSTANT_FLOAT64_PI;
118+
}
119+
return q;
120+
}

0 commit comments

Comments
 (0)