2015-07-19 12:12:37 -04:00
|
|
|
#include "picrin.h"
|
2016-02-20 10:58:58 -05:00
|
|
|
#include "picrin/extra.h"
|
2015-07-19 12:12:37 -04:00
|
|
|
|
2015-07-19 13:19:41 -04:00
|
|
|
#include <math.h>
|
|
|
|
|
2015-07-19 12:18:57 -04:00
|
|
|
static pic_value
|
|
|
|
pic_number_floor2(pic_state *pic)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
bool e1, e2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "II", &i, &e1, &j, &e2);
|
|
|
|
|
|
|
|
if (e1 && e2) {
|
|
|
|
int k;
|
|
|
|
|
|
|
|
k = (i < 0 && j < 0) || (0 <= i && 0 <= j)
|
|
|
|
? i / j
|
|
|
|
: (i / j) - 1;
|
|
|
|
|
2016-02-19 03:38:49 -05:00
|
|
|
return pic_return(pic, 2, pic_int_value(pic, k), pic_int_value(pic, i - k * j));
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
|
|
|
double q, r;
|
|
|
|
|
|
|
|
q = floor((double)i/j);
|
|
|
|
r = i - j * q;
|
2016-02-19 03:38:49 -05:00
|
|
|
return pic_return(pic, 2, pic_float_value(pic, q), pic_float_value(pic, r));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_trunc2(pic_state *pic)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
bool e1, e2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "II", &i, &e1, &j, &e2);
|
|
|
|
|
|
|
|
if (e1 && e2) {
|
2016-02-19 03:38:49 -05:00
|
|
|
return pic_return(pic, 2, pic_int_value(pic, i/j), pic_int_value(pic, i - (i/j) * j));
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
|
|
|
double q, r;
|
|
|
|
|
|
|
|
q = trunc((double)i/j);
|
|
|
|
r = i - j * q;
|
|
|
|
|
2016-02-19 03:38:49 -05:00
|
|
|
return pic_return(pic, 2, pic_float_value(pic, q), pic_float_value(pic, r));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_floor(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
bool e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "F", &f, &e);
|
|
|
|
|
|
|
|
if (e) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, (int)f);
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, floor(f));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_ceil(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
bool e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "F", &f, &e);
|
|
|
|
|
|
|
|
if (e) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, (int)f);
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, ceil(f));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_trunc(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
bool e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "F", &f, &e);
|
|
|
|
|
|
|
|
if (e) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, (int)f);
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, trunc(f));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_round(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
bool e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "F", &f, &e);
|
|
|
|
|
|
|
|
if (e) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, (int)f);
|
2015-07-19 12:18:57 -04:00
|
|
|
} else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, round(f));
|
2015-07-19 12:18:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-19 12:12:37 -04:00
|
|
|
static pic_value
|
|
|
|
pic_number_finite_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
if (pic_int_p(pic, v))
|
|
|
|
return pic_true_value(pic);
|
|
|
|
if (pic_float_p(pic, v) && ! (isinf(pic_float(pic, v)) || isnan(pic_float(pic, v))))
|
|
|
|
return pic_true_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
else
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_false_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_infinite_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
if (pic_float_p(pic, v) && isinf(pic_float(pic, v)))
|
|
|
|
return pic_true_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
else
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_false_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_nan_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
if (pic_float_p(pic, v) && isnan(pic_float(pic, v)))
|
|
|
|
return pic_true_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
else
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_false_value(pic);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_exp(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, exp(f));
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_log(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f,g;
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
argc = pic_get_args(pic, "f|f", &f, &g);
|
|
|
|
if (argc == 1) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, log(f));
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, log(f) / log(g));
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_sin(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
f = sin(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_cos(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
f = cos(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_tan(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
f = tan(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_acos(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
f = acos(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_asin(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
f = asin(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_atan(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f,g;
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
argc = pic_get_args(pic, "f|f", &f, &g);
|
|
|
|
if (argc == 1) {
|
|
|
|
f = atan(f);
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, f);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, atan2(f,g));
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_sqrt(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
pic_get_args(pic, "f", &f);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, sqrt(f));
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:24:35 -04:00
|
|
|
static pic_value
|
|
|
|
pic_number_abs(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
bool e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "F", &f, &e);
|
|
|
|
|
|
|
|
if (e) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, f < 0 ? -f : f);
|
2015-07-19 12:24:35 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, fabs(f));
|
2015-07-19 12:24:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_number_expt(pic_state *pic)
|
|
|
|
{
|
|
|
|
double f, g, h;
|
|
|
|
bool e1, e2;
|
|
|
|
|
|
|
|
pic_get_args(pic, "FF", &f, &e1, &g, &e2);
|
|
|
|
|
|
|
|
h = pow(f, g);
|
|
|
|
if (e1 && e2) {
|
|
|
|
if (h <= INT_MAX) {
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, (int)h);
|
2015-07-19 12:24:35 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_float_value(pic, h);
|
2015-07-19 12:24:35 -04:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:12:37 -04:00
|
|
|
void
|
|
|
|
pic_init_math(pic_state *pic)
|
|
|
|
{
|
2016-02-18 03:39:32 -05:00
|
|
|
pic_deflibrary(pic, "picrin.math");
|
2016-02-14 23:20:26 -05:00
|
|
|
|
|
|
|
pic_defun(pic, "floor/", pic_number_floor2);
|
|
|
|
pic_defun(pic, "truncate/", pic_number_trunc2);
|
|
|
|
pic_defun(pic, "floor", pic_number_floor);
|
|
|
|
pic_defun(pic, "ceiling", pic_number_ceil);
|
|
|
|
pic_defun(pic, "truncate", pic_number_trunc);
|
|
|
|
pic_defun(pic, "round", pic_number_round);
|
|
|
|
|
|
|
|
pic_defun(pic, "finite?", pic_number_finite_p);
|
|
|
|
pic_defun(pic, "infinite?", pic_number_infinite_p);
|
|
|
|
pic_defun(pic, "nan?", pic_number_nan_p);
|
|
|
|
pic_defun(pic, "sqrt", pic_number_sqrt);
|
|
|
|
pic_defun(pic, "exp", pic_number_exp);
|
|
|
|
pic_defun(pic, "log", pic_number_log);
|
|
|
|
pic_defun(pic, "sin", pic_number_sin);
|
|
|
|
pic_defun(pic, "cos", pic_number_cos);
|
|
|
|
pic_defun(pic, "tan", pic_number_tan);
|
|
|
|
pic_defun(pic, "acos", pic_number_acos);
|
|
|
|
pic_defun(pic, "asin", pic_number_asin);
|
|
|
|
pic_defun(pic, "atan", pic_number_atan);
|
|
|
|
pic_defun(pic, "abs", pic_number_abs);
|
|
|
|
pic_defun(pic, "expt", pic_number_expt);
|
2015-07-19 12:12:37 -04:00
|
|
|
}
|