forked from ground/ground
Organisation, documentation, mathlib
This commit is contained in:
79
extlibs/math/math.cpp
Normal file
79
extlibs/math/math.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "ground_lib.h"
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
// Math functions
|
||||
GroundValue sinVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(sin(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue cosVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(cos(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue tanVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(tan(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue sqrtVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(sqrt(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue modVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_2(GROUND_INT, GROUND_INT);
|
||||
return GROUND_INT_VAL(GET_INT(args[0]) % GET_INT(args[1]));
|
||||
}
|
||||
|
||||
GroundValue floorVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(floor(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue ceilVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(ceil(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue roundVal(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_1(GROUND_DOUBLE);
|
||||
return GROUND_DOUBLE_VAL(round(GET_DOUBLE(args[0])));
|
||||
}
|
||||
|
||||
GroundValue randomInt(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_2(GROUND_INT, GROUND_INT);
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> distrib(GET_INT(args[0]), GET_INT(args[1]));
|
||||
return GROUND_INT_VAL(distrib(gen));
|
||||
}
|
||||
|
||||
GroundValue randomDouble(GroundValue* args, int arg_count) {
|
||||
VALIDATE_ARGS_2(GROUND_DOUBLE, GROUND_DOUBLE);
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<> distrib(GET_DOUBLE(args[0]), GET_DOUBLE(args[1]));
|
||||
return GROUND_DOUBLE_VAL(distrib(gen));
|
||||
}
|
||||
|
||||
// Library setup
|
||||
GROUND_LIBRARY_INTERFACE()
|
||||
|
||||
GROUND_LIBRARY_INIT()
|
||||
REGISTER_GROUND_FUNCTION(sinVal);
|
||||
REGISTER_GROUND_FUNCTION(cosVal);
|
||||
REGISTER_GROUND_FUNCTION(tanVal);
|
||||
REGISTER_GROUND_FUNCTION(sqrtVal);
|
||||
REGISTER_GROUND_FUNCTION(modVal);
|
||||
REGISTER_GROUND_FUNCTION(floorVal);
|
||||
REGISTER_GROUND_FUNCTION(ceilVal);
|
||||
REGISTER_GROUND_FUNCTION(roundVal);
|
||||
REGISTER_GROUND_FUNCTION(randomInt);
|
||||
REGISTER_GROUND_FUNCTION(randomDouble);
|
||||
GROUND_LIBRARY_INIT_END()
|
||||
|
||||
GROUND_LIBRARY_CLEANUP()
|
||||
GROUND_LIBRARY_CLEANUP_END()
|
Reference in New Issue
Block a user