From d08789835930351d51aec01c36f917089205e10f Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 26 Jun 2021 19:23:02 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2math=20trig=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/arithm.c | 21 +++++++++++++++++++++ src/builtins/sysfn.c | 2 ++ 2 files changed, 23 insertions(+) diff --git a/src/builtins/arithm.c b/src/builtins/arithm.c index 30d498c4..ffe2284d 100644 --- a/src/builtins/arithm.c +++ b/src/builtins/arithm.c @@ -1,6 +1,7 @@ #include "../core.h" #include "../utils/each.h" #include "../utils/builtins.h" +#include "../nfns.h" #include static inline B arith_recm(BB2B f, B x) { @@ -21,6 +22,12 @@ B floor_c1(B t, B x) { if (isF64(x)) return m_f64(floor(x.f)); P1(floor); thrM(" B ceil_c1(B t, B x) { if (isF64(x)) return m_f64( ceil(x.f)); P1( ceil); thrM("⌈: Argument was not a number"); } B stile_c1(B t, B x) { if (isF64(x)) return m_f64( fabs(x.f)); P1(stile); thrM("|: Argument was not a number"); } B log_c1(B t, B x) { if (isF64(x)) return m_f64( log(x.f)); P1( log); thrM("⋆⁼: Getting log of non-number"); } +B sin_c1(B t, B x) { if (isF64(x)) return m_f64( sin(x.f)); P1( sin); thrM("•math.Sin: Argument contained non-number"); } +B cos_c1(B t, B x) { if (isF64(x)) return m_f64( cos(x.f)); P1( cos); thrM("•math.Cos: Argument contained non-number"); } +B tan_c1(B t, B x) { if (isF64(x)) return m_f64( tan(x.f)); P1( tan); thrM("•math.Tan: Argument contained non-number"); } +B asin_c1(B t, B x) { if (isF64(x)) return m_f64( asin(x.f)); P1( asin); thrM("•math.Asin: Argument contained non-number"); } +B acos_c1(B t, B x) { if (isF64(x)) return m_f64( acos(x.f)); P1( acos); thrM("•math.Acos: Argument contained non-number"); } +B atan_c1(B t, B x) { if (isF64(x)) return m_f64( atan(x.f)); P1( atan); thrM("•math.Atan: Argument contained non-number"); } #undef P1 B lt_c1(B t, B x) { return m_unit(x); } @@ -28,6 +35,20 @@ B eq_c1(B t, B x) { B r = m_i32(isArr(x)? rnk(x) : 0); decR(x); return r; } B ne_c1(B t, B x) { B r = m_f64(isArr(x)&&rnk(x)? *a(x)->sh : 1); decR(x); return r; } +static B internalNS; +B getMathNS() { + if (internalNS.u == 0) { + #define F(X,N) m_nfn(registerNFn(m_str32(U"•math." N), X##_c1, c2_invalid),m_f64(0)), + B fn = bqn_exec(m_str32(U"{⟨ Sin, Cos, Tan, Asin, Acos, Atan ⟩⇐𝕩}"), inc(bi_emptyCVec), inc(bi_emptySVec)); + B arg = m_caB(6, (B[]){F(sin,U"Sin")F(cos,U"Cos")F(tan,U"Tan")F(asin,U"Asin")F(acos,U"Acos")F(atan,U"Atan")}); + #undef F + internalNS = c1(fn,arg); + gc_add(internalNS); + dec(fn); + } + return inc(internalNS); +} + void arith_init() { c(BFn,bi_add)->ident = c(BFn,bi_sub)->ident = c(BFn,bi_or )->ident = c(BFn,bi_ne)->ident = c(BFn,bi_gt)->ident = m_i32(0); c(BFn,bi_mul)->ident = c(BFn,bi_div)->ident = c(BFn,bi_and)->ident = c(BFn,bi_eq)->ident = c(BFn,bi_ge)->ident = c(BFn,bi_pow)->ident = c(BFn,bi_not)->ident = m_i32(1); diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index b5e68dd1..4b62a473 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -293,6 +293,7 @@ B exit_c1(B t, B x) { } B getInternalNS(); +B getMathNS(); static B file_nsGen; B sys_c1(B t, B x) { @@ -316,6 +317,7 @@ B sys_c1(B t, B x) { r.a[i] = inc(fileNS); } else if (eqStr(c, U"internal")) r.a[i] = getInternalNS(); + else if (eqStr(c, U"math")) r.a[i] = getMathNS(); else if (eqStr(c, U"type")) r.a[i] = inc(bi_type); else if (eqStr(c, U"decompose")) r.a[i] = inc(bi_decp); else if (eqStr(c, U"primind")) r.a[i] = inc(bi_primInd);