From 798b06ddbf49e8bde1aa2c4edb2f71e66ca11d5d Mon Sep 17 00:00:00 2001 From: Oldes Date: Fri, 28 Feb 2020 15:14:12 +0100 Subject: [PATCH] FEAT: included `to-degrees` and `to-radians` native functions related to: https://github.com/Oldes/Rebol-issues/issues/2408 --- src/core/n-math.c | 29 +++++++++++++++++++++++++++++ src/tests/units/decimal-test.r3 | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/core/n-math.c b/src/core/n-math.c index c628d1e182..28fb8ed4ec 100644 --- a/src/core/n-math.c +++ b/src/core/n-math.c @@ -490,6 +490,35 @@ enum {SINE, COSINE, TANGENT}; return R_ARG1; } +/*********************************************************************** +** +*/ REBNATIVE(to_radians) +/* +// to-radians: native [ +// "Converts degrees to radians" +// degrees [integer! decimal!] "Degrees to convert" +// ] +***********************************************************************/ +{ + REBDEC degrees = AS_DECIMAL(D_ARG(1)); + SET_DECIMAL(D_RET, degrees * pi1 / 180.0 ); + return R_RET; +} + +/*********************************************************************** +** +*/ REBNATIVE(to_degrees) +/* +// to-degrees: native [ +// "Converts radians to degrees" +// radians [integer! decimal!] "Radians to convert" +// ] +***********************************************************************/ +{ + REBDEC radians = AS_DECIMAL(D_ARG(1)); + SET_DECIMAL(D_RET, radians * 180.0 / pi1 ); + return R_RET; +} /*********************************************************************** ** diff --git a/src/tests/units/decimal-test.r3 b/src/tests/units/decimal-test.r3 index 2c16768cdb..e122a3d821 100644 --- a/src/tests/units/decimal-test.r3 +++ b/src/tests/units/decimal-test.r3 @@ -65,5 +65,26 @@ Rebol [ --assert -1.0 = round/half-ceiling -1.5 ===end-group=== + +===start-group=== "to-degrees & to-radians" +;@@ https://github.com/Oldes/Rebol-issues/issues/2408 + --test-- "to-degrees to-radians" + foreach [d r] [ + 0 0.0 + 30 0.5235987756 + 45 0.7853981634 + 60 1.0471975512 + 90 1.5707963268 + 120 2.0943951024 + 135 2.3561944902 + 150 2.6179938780 + 180 3.1415926536 + 270 4.7123889804 + 360 6.2831853072 + ][ + --assert r = round/to r: to-radians d 0.0000000001 + --assert d = to-degrees r + ] +===end-group=== ~~~end-file~~~