Files
cground/libs/math/math.c
2026-01-19 20:32:29 +11:00

77 lines
2.3 KiB
C

#include <groundext.h>
#include <math.h>
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");
}