From 14b091b691dde5c555946ec20403a91933fa395b Mon Sep 17 00:00:00 2001 From: cpunch Date: Mon, 15 Feb 2021 14:00:26 -0600 Subject: [PATCH] Added trig. functions to the math library --- src/cbaselib.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++- src/cbaselib.h | 1 + 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 4173669..a4f8a6d 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -662,17 +662,119 @@ int cosmoB_mCeil(CState *state, int nargs, CValue *args) { return 1; } +int cosmoB_mSin(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.sin() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.sin", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, sin(cosmoV_readNumber(args[0]))); + return 1; +} + +int cosmoB_mCos(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.cos() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.cos", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, cos(cosmoV_readNumber(args[0]))); + return 1; +} + +int cosmoB_mTan(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.tan() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.tan", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, tan(cosmoV_readNumber(args[0]))); + return 1; +} + +int cosmoB_mASin(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.asin() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.asin", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, asin(cosmoV_readNumber(args[0]))); + return 1; +} + +int cosmoB_mACos(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.acos() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.acos", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, acos(cosmoV_readNumber(args[0]))); + return 1; +} + +int cosmoB_mATan(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "math.atan() expected 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_NUMBER(args[0])) { + cosmoV_typeError(state, "math.atan", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + cosmoV_pushNumber(state, atan(cosmoV_readNumber(args[0]))); + return 1; +} + void cosmoB_loadMathLib(CState *state) { const char *identifiers[] = { "abs", "floor", - "ceil" + "ceil", + "sin", + "cos", + "tan", + "asin", + "acos", + "atan" }; CosmoCFunction mathLib[] = { cosmoB_mAbs, cosmoB_mFloor, - cosmoB_mCeil + cosmoB_mCeil, + cosmoB_mSin, + cosmoB_mCos, + cosmoB_mTan, + cosmoB_mASin, + cosmoB_mACos, + cosmoB_mATan }; // make math library object @@ -683,6 +785,10 @@ void cosmoB_loadMathLib(CState *state) { cosmoV_pushCFunction(state, mathLib[i]); } + cosmoV_pushString(state, "pi"); + cosmoV_pushNumber(state, acos(-1)); + i++; + // make the object and register it as a global to the state cosmoV_makeObject(state, i); cosmoV_register(state, 1); diff --git a/src/cbaselib.h b/src/cbaselib.h index 4994768..b269b4f 100644 --- a/src/cbaselib.h +++ b/src/cbaselib.h @@ -40,6 +40,7 @@ COSMO_API void cosmoB_loadStrLib(CState *state); - math.abs - math.floor - math.ceil + - math.tan */ COSMO_API void cosmoB_loadMathLib(CState *state);