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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2017-12-30 21:46:59 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2018-03-15 21:19:31 +00:00
|
|
|
/* Get a random number */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_rand(JanetArgs args) {
|
|
|
|
JANET_FIXARITY(args, 0);
|
2018-03-15 21:19:31 +00:00
|
|
|
double r = (rand() % RAND_MAX) / ((double) RAND_MAX);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN_REAL(args, r);
|
2018-03-15 21:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Seed the random number generator */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_srand(JanetArgs args) {
|
2018-05-10 03:43:56 +00:00
|
|
|
int32_t x = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_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-09-06 02:18:42 +00:00
|
|
|
int janet_int(JanetArgs args) {
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
switch (janet_type(args.v[0])) {
|
2017-12-30 21:46:59 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "could not convert to integer");
|
|
|
|
case JANET_REAL:
|
|
|
|
*args.ret = janet_wrap_integer((int32_t) janet_unwrap_real(args.v[0]));
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_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-09-06 02:18:42 +00:00
|
|
|
int janet_real(JanetArgs args) {
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
switch (janet_type(args.v[0])) {
|
2017-12-30 21:46:59 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROW(args, "could not convert to real");
|
|
|
|
case JANET_REAL:
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = args.v[0];
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_INTEGER:
|
|
|
|
*args.ret = janet_wrap_real((double) janet_unwrap_integer(args.v[0]));
|
2017-12-30 21:46:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_remainder(JanetArgs args) {
|
|
|
|
JANET_FIXARITY(args, 2);
|
|
|
|
if (janet_checktype(args.v[0], JANET_INTEGER) &&
|
|
|
|
janet_checktype(args.v[1], JANET_INTEGER)) {
|
2018-07-02 04:12:36 +00:00
|
|
|
int32_t x, y;
|
2018-09-06 02:18:42 +00:00
|
|
|
x = janet_unwrap_integer(args.v[0]);
|
|
|
|
y = janet_unwrap_integer(args.v[1]);
|
|
|
|
JANET_RETURN_INTEGER(args, x % y);
|
2018-07-02 04:12:36 +00:00
|
|
|
} else {
|
|
|
|
double x, y;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_NUMBER(x, args, 0);
|
|
|
|
JANET_ARG_NUMBER(y, args, 1);
|
|
|
|
JANET_RETURN_REAL(args, fmod(x, y));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_DEFINE_MATHOP(name, fop)\
|
|
|
|
int janet_##name(JanetArgs args) {\
|
2018-05-10 03:43:56 +00:00
|
|
|
double x;\
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 1);\
|
|
|
|
JANET_ARG_NUMBER(x, args, 0);\
|
|
|
|
JANET_RETURN_REAL(args, fop(x));\
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_DEFINE_MATHOP(acos, acos)
|
|
|
|
JANET_DEFINE_MATHOP(asin, asin)
|
|
|
|
JANET_DEFINE_MATHOP(atan, atan)
|
|
|
|
JANET_DEFINE_MATHOP(cos, cos)
|
|
|
|
JANET_DEFINE_MATHOP(cosh, cosh)
|
|
|
|
JANET_DEFINE_MATHOP(sin, sin)
|
|
|
|
JANET_DEFINE_MATHOP(sinh, sinh)
|
|
|
|
JANET_DEFINE_MATHOP(tan, tan)
|
|
|
|
JANET_DEFINE_MATHOP(tanh, tanh)
|
|
|
|
JANET_DEFINE_MATHOP(exp, exp)
|
|
|
|
JANET_DEFINE_MATHOP(log, log)
|
|
|
|
JANET_DEFINE_MATHOP(log10, log10)
|
|
|
|
JANET_DEFINE_MATHOP(sqrt, sqrt)
|
|
|
|
JANET_DEFINE_MATHOP(ceil, ceil)
|
|
|
|
JANET_DEFINE_MATHOP(fabs, fabs)
|
|
|
|
JANET_DEFINE_MATHOP(floor, floor)
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_DEFINE_MATH2OP(name, fop)\
|
|
|
|
int janet_##name(JanetArgs args) {\
|
2018-05-10 03:43:56 +00:00
|
|
|
double lhs, rhs;\
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIXARITY(args, 2);\
|
|
|
|
JANET_ARG_NUMBER(lhs, args, 0);\
|
|
|
|
JANET_ARG_NUMBER(rhs, args, 1);\
|
|
|
|
JANET_RETURN_REAL(args, fop(lhs, rhs));\
|
2017-12-30 21:46:59 +00:00
|
|
|
}\
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_DEFINE_MATH2OP(atan2, atan2)
|
|
|
|
JANET_DEFINE_MATH2OP(pow, pow)
|
2018-01-16 01:14:21 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int janet_not(JanetArgs args) {
|
|
|
|
JANET_FIXARITY(args, 1);
|
|
|
|
JANET_RETURN_BOOLEAN(args, !janet_truthy(args.v[0]));
|
2018-02-09 16:57:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2018-11-15 20:45:41 +00:00
|
|
|
{"%", janet_remainder, NULL},
|
|
|
|
{"not", janet_not, NULL},
|
|
|
|
{"int", janet_int, NULL},
|
|
|
|
{"real", janet_real, NULL},
|
|
|
|
{"math.random", janet_rand, NULL},
|
|
|
|
{"math.seedrandom", janet_srand, NULL},
|
|
|
|
{"math.cos", janet_cos, NULL},
|
|
|
|
{"math.sin", janet_sin, NULL},
|
|
|
|
{"math.tan", janet_tan, NULL},
|
|
|
|
{"math.acos", janet_acos, NULL},
|
|
|
|
{"math.asin", janet_asin, NULL},
|
|
|
|
{"math.atan", janet_atan, NULL},
|
|
|
|
{"math.exp", janet_exp, NULL},
|
|
|
|
{"math.log", janet_log, NULL},
|
|
|
|
{"math.log10", janet_log10, NULL},
|
|
|
|
{"math.sqrt", janet_sqrt, NULL},
|
|
|
|
{"math.floor", janet_floor, NULL},
|
|
|
|
{"math.ceil", janet_ceil, NULL},
|
|
|
|
{"math.pow", janet_pow, NULL},
|
|
|
|
{NULL, NULL, NULL}
|
2018-01-19 21:43:19 +00:00
|
|
|
};
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Module entry point */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_lib_math(JanetArgs args) {
|
|
|
|
JanetTable *env = janet_env(args);
|
|
|
|
janet_cfuns(env, NULL, cfuns);
|
2018-01-19 21:43:19 +00:00
|
|
|
|
2018-11-15 20:45:41 +00:00
|
|
|
janet_def(env, "math.pi", janet_wrap_real(3.1415926535897931), NULL);
|
|
|
|
janet_def(env, "math.e", janet_wrap_real(2.7182818284590451), NULL);
|
|
|
|
janet_def(env, "math.inf", janet_wrap_real(INFINITY), NULL);
|
2018-01-16 01:14:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|