Skip to content

Commit 3a1fe1b

Browse files
feat: lib added
1 parent 0d8c569 commit 3a1fe1b

File tree

4 files changed

+309
-37
lines changed

4 files changed

+309
-37
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
'use strict';
20+
21+
/**
22+
* Compute the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
23+
*
24+
* @module @stdlib/math/base/special/atan2f
25+
*
26+
* @example
27+
* var atan2f = require( '@stdlib/math/base/special/atan2f' );
28+
*
29+
* var v = atan2f( 2.0, 2.0 ); // => atanf(1.0)
30+
* // returns ~0.785
31+
*
32+
* v = atan2f( 6.0, 2.0 ); // => atanf(3.0)
33+
* // returns ~1.249
34+
*
35+
* v = atan2f( -1.0, -1.0 ); // => atanf(1.0) - π
36+
* // returns ~-2.356
37+
*
38+
* v = atan2f( 3.0, 0.0 ); // => π/2
39+
* // returns ~1.571
40+
*
41+
* v = atan2f( -2.0, 0.0 ); // => -π/2
42+
* // returns ~-1.571
43+
*
44+
* v = atan2f( 0.0, 0.0 );
45+
* // returns 0.0
46+
*
47+
* v = atan2f( 3.0, NaN );
48+
* // returns NaN
49+
*
50+
* v = atan2f( NaN, 2.0 );
51+
* // returns NaN
52+
*/
53+
54+
// MODULES //
55+
56+
var atan2f = require( './main.js' );
57+
58+
59+
// EXPORTS //
60+
61+
module.exports = atan2f;
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 code, copyright and license are from [Go]{@link https://golang.org/src/math/atan2f.go}. The implementation follows the original, but has been modified for JavaScript.
22+
*
23+
* ```text
24+
* Copyright (c) 2009 The Go Authors. All rights reserved.
25+
*
26+
* Redistribution and use in source and binary forms, with or without
27+
* modification, are permitted provided that the following conditions are
28+
* met:
29+
*
30+
* * Redistributions of source code must retain the above copyright
31+
* notice, this list of conditions and the following disclaimer.
32+
* * Redistributions in binary form must reproduce the above
33+
* copyright notice, this list of conditions and the following disclaimer
34+
* in the documentation and/or other materials provided with the
35+
* distribution.
36+
* * Neither the name of Google Inc. nor the names of its
37+
* contributors may be used to endorse or promote products derived from
38+
* this software without specific prior written permission.
39+
*
40+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51+
* ```
52+
*/
53+
54+
'use strict';
55+
56+
// MODULES //
57+
58+
var isinfinitef = require( '@stdlib/math/base/assert/is-infinitef' );
59+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
60+
var copysignf = require( '@stdlib/math/base/special/copysignf' );
61+
var atan = require( '@stdlib/math/base/special/atan' );
62+
var PINF = require( '@stdlib/constants/float64/pinf' );
63+
var PI02F = require( '@stdlib/constants/float32/half-pi' );
64+
var PI04F = require( '@stdlib/constants/float32/fourth-pi' );
65+
var PI = require( '@stdlib/constants/float64/pi' );
66+
67+
68+
// MAIN //
69+
70+
/**
71+
* Computes the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
72+
*
73+
* @param {number} y - `y` coordinate
74+
* @param {number} x - `x` coordinate
75+
* @returns {number} angle (in radians)
76+
*
77+
* @example
78+
* var v = atan2f( 2.0, 2.0 ); // => atanf(1.0)
79+
* // returns ~0.785
80+
*
81+
* @example
82+
* var v = atan2f( 6.0, 2.0 ); // => atanf(3.0)
83+
* // returns ~1.249
84+
*
85+
* @example
86+
* var v = atan2f( -1.0, -1.0 ); // => atanf(1.0) - π
87+
* // returns ~-2.356
88+
*
89+
* @example
90+
* var v = atan2f( 3.0, 0.0 ); // => π/2
91+
* // returns ~1.571
92+
*
93+
* @example
94+
* var v = atan2f( -2.0, 0.0 ); // => -π/2
95+
* // returns ~-1.571
96+
*
97+
* @example
98+
* var v = atan2f( 0.0, 0.0 );
99+
* // returns 0.0
100+
*
101+
* @example
102+
* var v = atan2f( 3.0, NaN );
103+
* // returns NaN
104+
*
105+
* @example
106+
* var v = atan2f( NaN, 2.0 );
107+
* // returns NaN
108+
*/
109+
function atan2f( y, x ) {
110+
var q;
111+
if ( isnanf( x ) || isnanf( y ) ) {
112+
return NaN;
113+
}
114+
if ( isinfinitef( x ) ) {
115+
if ( x === PINF ) {
116+
if ( isinfinitef( y ) ) {
117+
return copysignf( PI04F, y );
118+
}
119+
return copysignf( 0.0, y );
120+
}
121+
// Case: x is -Infinity
122+
if ( isinfinitef( y ) ) {
123+
return copysignf( 3.0*PI04F, y );
124+
}
125+
return copysignf( PI, y );
126+
}
127+
if ( isinfinitef( y ) ) {
128+
return copysignf( PI02F, y );
129+
}
130+
if ( y === 0.0 ) {
131+
if ( x >= 0.0 ) {
132+
return copysignf( 0.0, y );
133+
}
134+
return copysignf( PI, y );
135+
}
136+
if ( x === 0.0 ) {
137+
return copysignf( PI02F, y );
138+
}
139+
q = atan( y / x );
140+
if ( x < 0.0 ) {
141+
if ( q <= 0.0 ) {
142+
return q + PI;
143+
}
144+
return q - PI;
145+
}
146+
return q;
147+
}
148+
149+
150+
// EXPORTS //
151+
152+
module.exports = atan2f;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var addon = require( './../src/addon.node' );
24+
25+
/**
26+
* Computes the angle in the plane (in radians) between the positive x-axis and the ray from `(0,0)` to the point `(x,y)`.
27+
*
28+
* @private
29+
* @param {number} y - `y` coordinate
30+
* @param {number} x - `x` coordinate
31+
* @returns {number} angle (in radians)
32+
*
33+
* @example
34+
* var v = atan2f( 2.0, 2.0 ); // => atanf(1.0)
35+
* // returns ~0.785
36+
*
37+
* @example
38+
* v = atan2f( 6.0, 2.0 ); // => atanf(3.0)
39+
* // returns ~1.249
40+
*
41+
* @example
42+
* v = atan2f( 3.0, 0.0 ); // => π/2
43+
* // returns ~1.571
44+
*
45+
* @example
46+
* v = atan2f( 3.0, NaN );
47+
* // returns NaN
48+
*/
49+
function atan2f( y, x ) {
50+
return addon( y, x );
51+
}
52+
53+
54+
// EXPORTS //
55+
56+
module.exports = atan2f;

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

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
* ```
3131
*/
3232

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"
33+
#include "stdlib/math/base/assert/is_infinitef.h"
34+
#include "stdlib/math/base/special/copysignf.h"
35+
#include "stdlib/number/float64/base/signbitf.h"
3636
#include "stdlib/math/base/assert/is_nanf.h"
37-
#include "stdlib/math/base/special/atan2.h"
37+
#include "stdlib/math/base/special/atanf.h"
38+
#include "stdlib/constants/float32/half_pi.h"
39+
#include "stdlib/constants/float32/fourth_pi.h"
3840
#include "stdlib/constants/float64/pinf.h"
3941
#include "stdlib/constants/float64/pi.h"
4042
#include <stdint.h>
@@ -49,69 +51,70 @@
4951
* @return angle (in radians)
5052
*
5153
* @example
52-
* float v = stdlib_base_atan2f( 2.0, 2.0 ); // => stdlib_base_atanf(1.0f)
53-
* // returns ~0.785f
54+
* float v = stdlib_base_atan2f( 2.0, 2.0 ); // => stdlib_base_atanf(1.0)
55+
* // returns ~0.785
5456
*
5557
* @example
56-
* float v = stdlib_base_atan2f( 6.0, 2.0 ); // => stdlib_base_atanf(3.0f)
57-
* // returns ~1.249f
58+
* float v = stdlib_base_atan2f( 6.0, 2.0 ); // => stdlib_base_atanf(3.0)
59+
* // returns ~1.249
5860
*
5961
* @example
60-
* float v = stdlib_base_atan2f( -1.0, -1.0f ); // => stdlib_base_atanf(1.0f) - π
61-
* // returns ~-2.356f
62+
* float v = stdlib_base_atan2f( -1.0, -1.0 ); // => stdlib_base_atanf(1.0) - π
63+
* // returns ~-2.356
6264
*
6365
* @example
64-
* float v = stdlib_base_atan2f( 3.0f, 0.0f ); // => π/2
65-
* // returns ~1.571ff
66+
* float v = stdlib_base_atan2f( 3.0, 0.0 ); // => π/2
67+
* // returns ~1.571
68+
*
6669
* @example
67-
* float v = stdlib_base_atan2f( -2.0f, 0.0f ); // => -π/2
68-
* // returns ~-1.571f
70+
* float v = stdlib_base_atan2f( -2.0, 0.0 ); // => -π/2
71+
* // returns ~-1.571
6972
*
7073
* @example
71-
* float v = stdlib_base_atan2f( 0.0f, 0.0f );
72-
* // returns 0.0f
74+
* float v = stdlib_base_atan2f( 0.0, 0.0 );
75+
* // returns 0.0
7376
*
7477
* @example
75-
* float v = stdlib_base_atan2f( 3.0f, NaN );
78+
* float v = stdlib_base_atan2f( 3.0, NaN );
7679
* // returns NaN
7780
*
7881
* @example
79-
* float v = stdlib_base_atan2f( NaN, 2.0f );
82+
* float v = stdlib_base_atan2f( NaN, 2.0 );
8083
* // returns NaN
8184
*/
8285
float stdlib_base_atan2f( const float y, const float x ) {
8386
float q;
8487
if ( stdlib_base_is_nanf( x ) || stdlib_base_is_nanf( y ) ) {
85-
return 0.0f / 0.0f; // float
88+
return 0.0 / 0.0; // NaN
8689
}
87-
if ( stdlib_base_is_infinite( x ) ) {
90+
if ( stdlib_base_is_infinitef( x ) ) {
8891
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 );
92+
if ( stdlib_base_is_infinitef( y ) ) {
93+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT32_HALF_PI, y );
9194
}
92-
return stdlib_base_copysign( 0.0f, y );
95+
return stdlib_base_copysignf( 0.0, y );
9396
}
9497
// 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 );
98+
if ( stdlib_base_is_infinitef( y ) ) {
99+
return stdlib_base_copysignf( 3.0 * STDLIB_CONSTANT_FLOAT32_HALF_PI, y );
97100
}
98-
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI, y );
101+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT64_PI, y );
99102
}
100-
if ( stdlib_base_is_infinite( y ) ) {
101-
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI / 2.0f, y );
103+
if ( stdlib_base_is_infinitef( y ) ) {
104+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT64_PI / 2.0, y );
102105
}
103-
if ( y == 0.0f ) {
104-
if ( x >= 0.0f && !stdlib_base_float64_signbit( x ) ) {
105-
return stdlib_base_copysign( 0.0, y );
106+
if ( y == 0.0 ) {
107+
if ( x >= 0.0 && !stdlib_base_float64_signbitf( x ) ) {
108+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT32_HALF_PI, y );
106109
}
107-
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI, y );
110+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT64_PI, y );
108111
}
109-
if ( x == 0.0f ) {
110-
return stdlib_base_copysign( STDLIB_CONSTANT_FLOAT64_PI / 2.0f, y );
112+
if ( x == 0.0 ) {
113+
return stdlib_base_copysignf( STDLIB_CONSTANT_FLOAT32_FOURTH_PI, y );
111114
}
112-
q = stdlib_base_atan2( y / x );
113-
if ( x < 0.0f ) {
114-
if ( q <= 0.0f ) {
115+
q = stdlib_base_atan( y / x );
116+
if ( x < 0.0 ) {
117+
if ( q <= 0.0 ) {
115118
return q + STDLIB_CONSTANT_FLOAT64_PI;
116119
}
117120
return q - STDLIB_CONSTANT_FLOAT64_PI;

0 commit comments

Comments
 (0)