Skip to content

Commit cd09db2

Browse files
committed
FEAT: new natives ARCTANGENT2 and ATAN2
ATAN2 is direct mapping to libc's ATAN2 function without any conversion so does not accept integer types as an input. ARCTANGENT2 is more user friendly (but little bit slower) - it uses pair! type as an input of the coordinates and result is in degrees by default (may be changed using /radians refinement) Example output: ``` >> arctangent2 10x-10 == -45.0 >> arctangent2/radians 10x-10 == -0.7853981633974483 >> atan2 -10.0 10.0 == -0.7853981633974483 ``` NOTE: notice that ATAN2 is using the input in classic order (Y and X) Implements wish: metaeducation/rebol-issues#882
1 parent 8feac42 commit cd09db2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/core/n-math.c

+32
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ enum {SINE, COSINE, TANGENT};
183183
return R_RET;
184184
}
185185

186+
/***********************************************************************
187+
**
188+
*/ REBNATIVE(arctangent2)
189+
/*
190+
// arctangent2: native [
191+
// {Returns the angle of the point, when measured counterclockwise from a circle's X axis (where 0x0 represents the center of the circle). The return value is in interval -180 to 180 degrees.}
192+
// point [pair!] "X/Y coordinate in space"
193+
// /radians "Result is in radians instead of degrees"
194+
// ]
195+
***********************************************************************/
196+
{
197+
REBDEC dval = atan2(VAL_PAIR_Y(D_ARG(1)), VAL_PAIR_X(D_ARG(1)));
198+
if(!D_REF(2)) dval *= 180.0 / pi1; //to degrees
199+
SET_DECIMAL(D_RET, dval);
200+
return R_RET;
201+
}
202+
186203

187204
// Follows faster trigonometric functions (without conversions and bounds checks)
188205
#ifndef USE_NO_INFINITY //use these functions only with INFINITY support (cos/sin may return 1.#NaN value)!
@@ -271,6 +288,21 @@ enum {SINE, COSINE, TANGENT};
271288
return R_RET;
272289
}
273290

291+
/***********************************************************************
292+
**
293+
*/ REBNATIVE(atan2)
294+
/*
295+
// atan2: native [
296+
// {Returns the angle of the point y/x in the interval [-pi,+pi] radians.}
297+
// y [decimal!] "The proportion of the Y-coordinate"
298+
// x [decimal!] "The proportion of the X-coordinate"
299+
// ]
300+
***********************************************************************/
301+
{
302+
SET_DECIMAL(D_RET, atan2(VAL_DECIMAL(D_ARG(1)), VAL_DECIMAL(D_ARG(2))));
303+
return R_RET;
304+
}
305+
274306
#endif //!USE_NO_INFINITY
275307

276308

0 commit comments

Comments
 (0)