From 0f155c80be618d9a70e14935eceef9e9c3e225c5 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Wed, 21 Jan 2026 07:23:24 +1100 Subject: [PATCH] ground library version 1.1.5 --- libs/math/math.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/libs/math/math.c b/libs/math/math.c index 36753b4..6969778 100644 --- a/libs/math/math.c +++ b/libs/math/math.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include GroundValue ground_sin(GroundScope* scope, List args) { @@ -101,6 +103,62 @@ GroundValue ground_clamp(GroundScope* scope, List args) { ); } +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 + ); +} + GroundValue ground_random(GroundScope* scope, List args) { int64_t min = args.values[0].data.intVal; int64_t max = args.values[1].data.intVal; @@ -115,7 +173,7 @@ GroundValue ground_random_double(GroundScope* scope, List args) { double max = args.values[1].data.doubleVal; return groundCreateValue( - INT, + DOUBLE, min + (double)rand() / RAND_MAX * (max - min) ); } @@ -128,7 +186,8 @@ GroundValue ground_random_set_seed(GroundScope* scope, List args) { } void ground_init(GroundScope* scope) { - srand(time(NULL)); + srand((unsigned)time(NULL) ^ (unsigned)clock()); + 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"); @@ -144,6 +203,10 @@ void ground_init(GroundScope* scope) { 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"); + 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"); + 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");