diff --git a/libs/math/math.c b/libs/math/math.c new file mode 100644 index 0000000..b48744c --- /dev/null +++ b/libs/math/math.c @@ -0,0 +1,77 @@ +#include +#include + +GroundValue ground_sin(GroundScope* scope, List args) { + return groundCreateValue( + DOUBLE, + sin(args.values[0].data.doubleVal) + ); +} +GroundValue ground_cos(GroundScope* scope, List args) { + return groundCreateValue( + DOUBLE, + cos(args.values[0].data.doubleVal) + ); +} +GroundValue ground_tan(GroundScope* scope, List args) { + return groundCreateValue( + DOUBLE, + tan(args.values[0].data.doubleVal) + ); +} + +GroundValue rad_to_deg(GroundScope* scope, List args) { + double radians = args.values[0].data.doubleVal; + + return groundCreateValue( + DOUBLE, + radians * (180.0 / M_PI) + ); +} +GroundValue deg_to_rad(GroundScope* scope, List args) { + double deg = args.values[0].data.doubleVal; + + return groundCreateValue( + DOUBLE, + deg * (M_PI / 180.0) + ); +} + +GroundValue ground_modulos(GroundScope* scope, List args) { + return groundCreateValue( + DOUBLE, + fmod(args.values[0].data.doubleVal, args.values[1].data.doubleVal) + ); +} +GroundValue ground_pow(GroundScope* scope, List args) { + return groundCreateValue( + DOUBLE, + pow(args.values[0].data.doubleVal, args.values[1].data.doubleVal) + ); +} +GroundValue ground_sqrt(GroundScope* scope, List args) { + double value = args.values[0].data.doubleVal; + + if (value < 0) { + ERROR("Can't get square root of number less than 0.", "InvalidMath"); + } + + return groundCreateValue( + DOUBLE, + sqrt(value) + ); +} + +void ground_init(GroundScope* scope) { + groundAddNativeFunction(scope, "math_Sin", ground_sin, DOUBLE, 1, DOUBLE, "radians"); + groundAddNativeFunction(scope, "math_Cos", ground_cos, DOUBLE, 1, DOUBLE, "radians"); + groundAddNativeFunction(scope, "math_Tan", ground_tan, DOUBLE, 1, DOUBLE, "radians"); + + groundAddNativeFunction(scope, "math_DegreesToRadians", deg_to_rad, DOUBLE, 1, DOUBLE, "degrees"); + groundAddNativeFunction(scope, "math_RadiansToDegrees", rad_to_deg, DOUBLE, 1, DOUBLE, "radians"); + + groundAddNativeFunction(scope, "math_Modulos", ground_modulos, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2"); + groundAddNativeFunction(scope, "math_Pow", ground_pow, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2"); + + groundAddNativeFunction(scope, "math_Sqrt", ground_sqrt, DOUBLE, 1, DOUBLE, "number"); +} \ No newline at end of file