#include <mruby.h>
#ifdef MRB_NO_FLOAT
# error Math conflicts with 'MRB_NO_FLOAT' configuration
#endif
#include <mruby/array.h>
#include <mruby/presym.h>
static void
domain_error(mrb_state *mrb, const char *func)
{
struct RClass *math = mrb_module_get_id(mrb, MRB_SYM(Math));
struct RClass *domainerror = mrb_class_get_under_id(mrb, math, MRB_SYM(DomainError));
mrb_raisef(mrb, domainerror, "Numerical argument is out of domain - %s", func);
}
#if defined _MSC_VER && _MSC_VER <= 1700
double
asinh(double x)
{
double xa, ya, y;
xa = fabs(x);
if (xa > 3.16227E+18) {
ya = log(xa) + 0.69314718055994530942;
}
else {
ya = log(xa + sqrt(xa*xa + 1.0));
}
y = _copysign(ya, x);
return y;
}
double
acosh(double x)
{
double y;
if (x > 3.16227E+18) {
y = log(x) + 0.69314718055994530942;
}
else {
y = log(x + sqrt(x*x - 1.0));
}
return y;
}
double
atanh(double x)
{
double y;
if (fabs(x) < 1E-2) {
double x2 = x * x;
y = x*(1.0 + x2*(1.0/3.0 + x2*(1.0/5.0 + x2*(1.0/7.0))));
}
else {
y = 0.5 * (log(1.0+x) - log(1.0-x));
}
return y;
}
double
cbrt(double x)
{
double xa, ya, y;
xa = fabs(x);
ya = pow(xa, 1.0/3.0);
y = _copysign(ya, x);
return y;
}
double erfc(double x);
double
erf(double x)
{
static const double two_sqrtpi = 1.128379167095512574;
double sum = x;
double term = x;
double xsqr = x*x;
int j= 1;
if (fabs(x) > 2.2) {
return 1.0 - erfc(x);
}
do {
term *= xsqr/j;
sum -= term/(2*j+1);
j++;
term *= xsqr/j;
sum += term/(2*j+1);
j++;
if (sum == 0) break;
} while (fabs(term/sum) > DBL_EPSILON);
return two_sqrtpi*sum;
}
double
erfc(double x)
{
static const double one_sqrtpi= 0.564189583547756287;
double a = 1;
double b = x;
double c = x;
double d = x*x+0.5;
double q1;
double q2 = b/d;
double n = 1.0;
double t;
if (fabs(x) < 2.2) {
return 1.0 - erf(x);
}
if (x < 0.0) {
return 2.0 - erfc(-x);
}
do {
t = a*n+b*x;
a = b;
b = t;
t = c*n+d*x;
c = d;
d = t;
n += 0.5;
q1 = q2;
q2 = b/d;
} while (fabs(q1-q2)/q2 > DBL_EPSILON);
return one_sqrtpi*exp(-x*x)*q2;
}
#endif
#if defined __FreeBSD__ && !defined __FreeBSD_version
#include <osreldate.h>
#endif
#if (defined _MSC_VER && _MSC_VER < 1800) || defined __ANDROID__ || (defined __FreeBSD__ && __FreeBSD_version < 803000)
double
log2(double x)
{
return log10(x)/log10(2.0);
}
#endif
static mrb_value
math_sin(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = sin(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_cos(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = cos(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_tan(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = tan(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_asin(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < -1.0 || x > 1.0) {
domain_error(mrb, "asin");
}
x = asin(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_acos(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < -1.0 || x > 1.0) {
domain_error(mrb, "acos");
}
x = acos(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_atan(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = atan(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_atan2(mrb_state *mrb, mrb_value obj)
{
mrb_float x, y;
mrb_get_args(mrb, "ff", &x, &y);
x = atan2(x, y);
return mrb_float_value(mrb, x);
}
static mrb_value
math_sinh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = sinh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_cosh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = cosh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_tanh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = tanh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_asinh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = asinh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_acosh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < 1.0) {
domain_error(mrb, "acosh");
}
x = acosh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_atanh(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < -1.0 || x > 1.0) {
domain_error(mrb, "atanh");
}
x = atanh(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_exp(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = exp(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_log(mrb_state *mrb, mrb_value obj)
{
mrb_float x, base;
mrb_int argc;
argc = mrb_get_args(mrb, "f|f", &x, &base);
if (x < 0.0) {
domain_error(mrb, "log");
}
x = log(x);
if (argc == 2) {
if (base < 0.0) {
domain_error(mrb, "log");
}
x /= log(base);
}
return mrb_float_value(mrb, x);
}
static mrb_value
math_log2(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < 0.0) {
domain_error(mrb, "log2");
}
x = log2(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_log10(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < 0.0) {
domain_error(mrb, "log10");
}
x = log10(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_sqrt(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
if (x < 0.0) {
domain_error(mrb, "sqrt");
}
x = sqrt(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_cbrt(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = cbrt(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_frexp(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
int exp;
mrb_get_args(mrb, "f", &x);
x = frexp(x, &exp);
return mrb_assoc_new(mrb, mrb_float_value(mrb, x), mrb_fixnum_value(exp));
}
static mrb_value
math_ldexp(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_int i;
mrb_get_args(mrb, "fi", &x, &i);
x = ldexp(x, (int)i);
return mrb_float_value(mrb, x);
}
static mrb_value
math_hypot(mrb_state *mrb, mrb_value obj)
{
mrb_float x, y;
mrb_get_args(mrb, "ff", &x, &y);
x = hypot(x, y);
return mrb_float_value(mrb, x);
}
static mrb_value
math_erf(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = erf(x);
return mrb_float_value(mrb, x);
}
static mrb_value
math_erfc(mrb_state *mrb, mrb_value obj)
{
mrb_float x;
mrb_get_args(mrb, "f", &x);
x = erfc(x);
return mrb_float_value(mrb, x);
}
void
mrb_mruby_math_gem_init(mrb_state* mrb)
{
struct RClass *mrb_math;
mrb_math = mrb_define_module(mrb, "Math");
mrb_define_class_under_id(mrb, mrb_math, MRB_SYM(DomainError), mrb->eStandardError_class);
#ifdef M_PI
mrb_define_const_id(mrb, mrb_math, MRB_SYM(PI), mrb_float_value(mrb, M_PI));
#else
mrb_define_const_id(mrb, mrb_math, MRB_SYM(PI), mrb_float_value(mrb, atan(1.0)*4.0));
#endif
#ifdef M_E
mrb_define_const_id(mrb, mrb_math, MRB_SYM(E), mrb_float_value(mrb, M_E));
#else
mrb_define_const_id(mrb, mrb_math, MRB_SYM(E), mrb_float_value(mrb, exp(1.0)));
#endif
mrb_define_module_function(mrb, mrb_math, "sin", math_sin, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "cos", math_cos, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "tan", math_tan, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "asin", math_asin, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "acos", math_acos, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "atan", math_atan, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "exp", math_exp, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "log", math_log, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
mrb_define_module_function(mrb, mrb_math, "log2", math_log2, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "log10", math_log10, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, mrb_math, "erf", math_erf, MRB_ARGS_REQ(1));
mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, MRB_ARGS_REQ(1));
}
void
mrb_mruby_math_gem_final(mrb_state* mrb)
{
}