2017-12-30 21:46:59 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-12-30 21:46:59 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2018-03-15 21:19:31 +00:00
|
|
|
/* Get a random number */
|
|
|
|
int dst_rand(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
2018-03-15 21:19:31 +00:00
|
|
|
double r = (rand() % RAND_MAX) / ((double) RAND_MAX);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_REAL(args, r);
|
2018-03-15 21:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Seed the random number generator */
|
|
|
|
int dst_srand(DstArgs args) {
|
2018-05-10 03:43:56 +00:00
|
|
|
int32_t x = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_INTEGER(x, args, 0);
|
2018-05-10 03:43:56 +00:00
|
|
|
srand((unsigned) x);
|
|
|
|
return 0;
|
2018-03-15 21:19:31 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Convert a number to an integer */
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_int(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
2018-01-14 17:10:45 +00:00
|
|
|
switch (dst_type(args.v[0])) {
|
2017-12-30 21:46:59 +00:00
|
|
|
default:
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "could not convert to integer");
|
2017-12-30 21:46:59 +00:00
|
|
|
case DST_REAL:
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = dst_wrap_integer((int32_t) dst_unwrap_real(args.v[0]));
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
case DST_INTEGER:
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = args.v[0];
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a number to a real number */
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_real(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
2018-01-14 17:10:45 +00:00
|
|
|
switch (dst_type(args.v[0])) {
|
2017-12-30 21:46:59 +00:00
|
|
|
default:
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "could not convert to real");
|
2017-12-30 21:46:59 +00:00
|
|
|
case DST_REAL:
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = args.v[0];
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
case DST_INTEGER:
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = dst_wrap_real((double) dst_unwrap_integer(args.v[0]));
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-02 04:12:36 +00:00
|
|
|
int dst_remainder(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
2018-07-02 04:12:36 +00:00
|
|
|
if (dst_checktype(args.v[0], DST_INTEGER) &&
|
|
|
|
dst_checktype(args.v[1], DST_INTEGER)) {
|
|
|
|
int32_t x, y;
|
|
|
|
x = dst_unwrap_integer(args.v[0]);
|
|
|
|
y = dst_unwrap_integer(args.v[1]);
|
|
|
|
DST_RETURN_INTEGER(args, x % y);
|
|
|
|
} else {
|
|
|
|
double x, y;
|
|
|
|
DST_ARG_NUMBER(x, args, 0);
|
|
|
|
DST_ARG_NUMBER(y, args, 1);
|
|
|
|
DST_RETURN_REAL(args, fmod(x, y));
|
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
#define DST_DEFINE_MATHOP(name, fop)\
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_##name(DstArgs args) {\
|
2018-05-10 03:43:56 +00:00
|
|
|
double x;\
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);\
|
|
|
|
DST_ARG_NUMBER(x, args, 0);\
|
|
|
|
DST_RETURN_REAL(args, fop(x));\
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DST_DEFINE_MATHOP(acos, acos)
|
|
|
|
DST_DEFINE_MATHOP(asin, asin)
|
|
|
|
DST_DEFINE_MATHOP(atan, atan)
|
|
|
|
DST_DEFINE_MATHOP(cos, cos)
|
|
|
|
DST_DEFINE_MATHOP(cosh, cosh)
|
|
|
|
DST_DEFINE_MATHOP(sin, sin)
|
|
|
|
DST_DEFINE_MATHOP(sinh, sinh)
|
|
|
|
DST_DEFINE_MATHOP(tan, tan)
|
|
|
|
DST_DEFINE_MATHOP(tanh, tanh)
|
|
|
|
DST_DEFINE_MATHOP(exp, exp)
|
|
|
|
DST_DEFINE_MATHOP(log, log)
|
|
|
|
DST_DEFINE_MATHOP(log10, log10)
|
|
|
|
DST_DEFINE_MATHOP(sqrt, sqrt)
|
|
|
|
DST_DEFINE_MATHOP(ceil, ceil)
|
|
|
|
DST_DEFINE_MATHOP(fabs, fabs)
|
|
|
|
DST_DEFINE_MATHOP(floor, floor)
|
|
|
|
|
|
|
|
#define DST_DEFINE_MATH2OP(name, fop)\
|
2018-01-14 17:10:45 +00:00
|
|
|
int dst_##name(DstArgs args) {\
|
2018-05-10 03:43:56 +00:00
|
|
|
double lhs, rhs;\
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);\
|
|
|
|
DST_ARG_NUMBER(lhs, args, 0);\
|
|
|
|
DST_ARG_NUMBER(rhs, args, 1);\
|
|
|
|
DST_RETURN_REAL(args, fop(lhs, rhs));\
|
2017-12-30 21:46:59 +00:00
|
|
|
}\
|
|
|
|
|
|
|
|
DST_DEFINE_MATH2OP(atan2, atan2)
|
|
|
|
DST_DEFINE_MATH2OP(pow, pow)
|
2018-01-16 01:14:21 +00:00
|
|
|
|
2018-02-09 16:57:58 +00:00
|
|
|
static int dst_not(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_RETURN_BOOLEAN(args, !dst_truthy(args.v[0]));
|
2018-02-09 16:57:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
static const DstReg cfuns[] = {
|
2018-07-02 04:12:36 +00:00
|
|
|
{"%", dst_remainder},
|
2018-02-09 16:57:58 +00:00
|
|
|
{"not", dst_not},
|
2018-07-02 04:12:36 +00:00
|
|
|
{"int", dst_int},
|
|
|
|
{"real", dst_real},
|
|
|
|
{"math.random", dst_rand},
|
|
|
|
{"math.seedrandom", dst_srand},
|
|
|
|
{"math.cos", dst_cos},
|
|
|
|
{"math.sin", dst_sin},
|
|
|
|
{"math.tan", dst_tan},
|
|
|
|
{"math.acos", dst_acos},
|
|
|
|
{"math.asin", dst_asin},
|
|
|
|
{"math.atan", dst_atan},
|
|
|
|
{"math.exp", dst_exp},
|
|
|
|
{"math.log", dst_log},
|
|
|
|
{"math.log10", dst_log10},
|
|
|
|
{"math.sqrt", dst_sqrt},
|
|
|
|
{"math.floor", dst_floor},
|
|
|
|
{"math.ceil", dst_ceil},
|
|
|
|
{"math.pow", dst_pow},
|
2018-01-19 21:43:19 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Module entry point */
|
2018-01-18 22:25:45 +00:00
|
|
|
int dst_lib_math(DstArgs args) {
|
2018-08-26 18:35:01 +00:00
|
|
|
DstTable *env = dst_env(args);
|
|
|
|
dst_cfuns(env, NULL, cfuns);
|
2018-01-19 21:43:19 +00:00
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
dst_def(env, "math.pi", dst_wrap_real(3.1415926535897931));
|
|
|
|
dst_def(env, "math.e", dst_wrap_real(2.7182818284590451));
|
|
|
|
dst_def(env, "math.inf", dst_wrap_real(INFINITY));
|
2018-01-16 01:14:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|