Files
cground/libs/math/math.c

213 lines
6.3 KiB
C
Raw Permalink Normal View History

2026-01-19 20:32:29 +11:00
#include <groundext.h>
#include <math.h>
2026-01-20 21:29:42 +11:00
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
2026-01-21 07:23:24 +11:00
#include <limits.h>
#include <float.h>
2026-01-20 21:29:42 +11:00
2026-01-19 20:32:29 +11:00
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) {
return groundCreateValue(
DOUBLE,
2026-01-20 21:29:42 +11:00
args.values[0].data.doubleVal * (180.0 / M_PI)
2026-01-19 20:32:29 +11:00
);
}
GroundValue deg_to_rad(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
2026-01-20 21:29:42 +11:00
args.values[0].data.doubleVal * (M_PI / 180.0)
2026-01-19 20:32:29 +11:00
);
}
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)
);
}
2026-01-20 21:29:42 +11:00
GroundValue ground_abs(GroundScope* scope, List args) {
return groundCreateValue(
DOUBLE,
abs(args.values[0].data.doubleVal)
);
}
GroundValue ground_min(GroundScope* scope, List args) {
double a = args.values[0].data.doubleVal;
double b = args.values[1].data.doubleVal;
return groundCreateValue(
DOUBLE,
(a < b) ? a : b
);
}
GroundValue ground_max(GroundScope* scope, List args) {
double a = args.values[0].data.doubleVal;
double b = args.values[1].data.doubleVal;
return groundCreateValue(
DOUBLE,
(a > b) ? a : b
);
}
GroundValue ground_clamp(GroundScope* scope, List args) {
double number = args.values[0].data.doubleVal;
double min = args.values[1].data.doubleVal;
double max = args.values[2].data.doubleVal;
if (number < min) number = min;
if (number > max) number = max;
return groundCreateValue(
DOUBLE,
number
);
}
2026-01-21 07:23:24 +11:00
GroundValue ground_round(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= 0.0)
return groundCreateValue(
INT,
(long long)(x + 0.5)
);
else
return groundCreateValue(
INT,
(long long)(x - 0.5)
);
}
GroundValue ground_floor(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
long long i = (long long)x; // truncates toward zero
if (x < 0 && x != (double)i) {
return groundCreateValue(
INT,
i-1
);
}
return groundCreateValue(
INT,
i
);
}
GroundValue ground_ceil(GroundScope* scope, List args) {
double x = args.values[0].data.doubleVal;
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
long long i = (long long)x; // truncates toward zero
if (x > 0 && x != (double)i) {
return groundCreateValue(
INT,
i+1
);
}
return groundCreateValue(
INT,
i
);
}
2026-01-20 21:29:42 +11:00
GroundValue ground_random(GroundScope* scope, List args) {
int64_t min = args.values[0].data.intVal;
int64_t max = args.values[1].data.intVal;
return groundCreateValue(
INT,
min + rand() % (max - min)
);
}
GroundValue ground_random_double(GroundScope* scope, List args) {
double min = args.values[0].data.doubleVal;
double max = args.values[1].data.doubleVal;
return groundCreateValue(
2026-01-21 07:23:24 +11:00
DOUBLE,
2026-01-20 21:29:42 +11:00
min + (double)rand() / RAND_MAX * (max - min)
);
}
GroundValue ground_random_set_seed(GroundScope* scope, List args) {
srand(args.values[0].data.intVal);
return groundCreateValue(
INT,
0
);
}
2026-01-19 20:32:29 +11:00
void ground_init(GroundScope* scope) {
2026-01-21 07:23:24 +11:00
srand((unsigned)time(NULL) ^ (unsigned)clock());
2026-01-19 20:32:29 +11:00
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");
2026-01-20 21:29:42 +11:00
groundAddNativeFunction(scope, "math_Abs", ground_abs, DOUBLE, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Min", ground_min, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Max", ground_max, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
groundAddNativeFunction(scope, "math_Clamp", ground_clamp, DOUBLE, 2, DOUBLE, "number", DOUBLE, "min", DOUBLE, "max");
2026-01-21 07:23:24 +11:00
groundAddNativeFunction(scope, "math_Round", ground_round, INT, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Floor", ground_floor, INT, 1, DOUBLE, "number");
groundAddNativeFunction(scope, "math_Ceil", ground_ceil, INT, 1, DOUBLE, "number");
2026-01-20 21:29:42 +11:00
groundAddNativeFunction(scope, "math_Random", ground_random, INT, 2, INT, "min", INT, "max");
groundAddNativeFunction(scope, "math_RandomDouble", ground_random_double, DOUBLE, 2, DOUBLE, "min", DOUBLE, "max");
groundAddNativeFunction(scope, "math_RandomSetSeed", ground_random_set_seed, INT, 1, INT, "seed");
2026-01-19 20:32:29 +11:00
}