Project

General

Profile

Feature #10800 » introduce_num2dbl_with_to_f_func.patch

gogotanaka (Kazuki Tanaka), 01/29/2015 10:08 PM

View differences:

math.c
VALUE rb_mMath;
VALUE rb_eMathDomainError;
#define fix_to_f_optimizable() rb_method_basic_definition_p(rb_cFixnum, id_to_f)
#define Get_Float(x) \
((RB_TYPE_P((x), T_FLOAT) ? (void)0 : ((x) = rb_to_float(x))), RFLOAT_VALUE(x))
#define Get_Double(x) \
(FIXNUM_P(x) && fix_to_f_optimizable() ? \
(double)FIX2LONG(x) : \
Get_Float(x))
static inline double
num2dbl_with_to_f(VALUE num)
{
switch (TYPE(num)) {
case T_FLOAT:
return RFLOAT_VALUE(num);
case T_FIXNUM:
if (rb_method_basic_definition_p(rb_cFixnum, id_to_f)) {
return (double)FIX2LONG(num);
}
else {
return RFLOAT_VALUE(rb_to_float(num));
}
case T_BIGNUM:
if (rb_method_basic_definition_p(rb_cBignum, id_to_f)) {
return rb_big2dbl(num);
}
else {
return RFLOAT_VALUE(rb_to_float(num));
}
default:
return RFLOAT_VALUE(rb_to_float(num));
}
}
#define domain_error(msg) \
rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
......
# define M_PI 3.14159265358979323846
#endif
double dx, dy;
dx = Get_Double(x);
dy = Get_Double(y);
dx = num2dbl_with_to_f(x);
dy = num2dbl_with_to_f(y);
if (dx == 0.0 && dy == 0.0) {
if (!signbit(dx))
return DBL2NUM(dy);
......
static VALUE
math_cos(VALUE obj, VALUE x)
{
return DBL2NUM(cos(Get_Double(x)));
return DBL2NUM(cos(num2dbl_with_to_f(x)));
}
/*
......
static VALUE
math_sin(VALUE obj, VALUE x)
{
return DBL2NUM(sin(Get_Double(x)));
return DBL2NUM(sin(num2dbl_with_to_f(x)));
}
......
static VALUE
math_tan(VALUE obj, VALUE x)
{
return DBL2NUM(tan(Get_Double(x)));
return DBL2NUM(tan(num2dbl_with_to_f(x)));
}
/*
......
{
double d0, d;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < -1.0 || 1.0 < d0) domain_error("acos");
d = acos(d0);
......
{
double d0, d;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < -1.0 || 1.0 < d0) domain_error("asin");
d = asin(d0);
......
static VALUE
math_atan(VALUE obj, VALUE x)
{
return DBL2NUM(atan(Get_Double(x)));
return DBL2NUM(atan(num2dbl_with_to_f(x)));
}
#ifndef HAVE_COSH
......
static VALUE
math_cosh(VALUE obj, VALUE x)
{
return DBL2NUM(cosh(Get_Double(x)));
return DBL2NUM(cosh(num2dbl_with_to_f(x)));
}
#ifndef HAVE_SINH
......
static VALUE
math_sinh(VALUE obj, VALUE x)
{
return DBL2NUM(sinh(Get_Double(x)));
return DBL2NUM(sinh(num2dbl_with_to_f(x)));
}
#ifndef HAVE_TANH
......
static VALUE
math_tanh(VALUE obj, VALUE x)
{
return DBL2NUM(tanh(Get_Double(x)));
return DBL2NUM(tanh(num2dbl_with_to_f(x)));
}
/*
......
{
double d0, d;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < 1.0) domain_error("acosh");
d = acosh(d0);
......
static VALUE
math_asinh(VALUE obj, VALUE x)
{
return DBL2NUM(asinh(Get_Double(x)));
return DBL2NUM(asinh(num2dbl_with_to_f(x)));
}
/*
......
{
double d0, d;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < -1.0 || +1.0 < d0) domain_error("atanh");
/* check for pole error */
......
static VALUE
math_exp(VALUE obj, VALUE x)
{
return DBL2NUM(exp(Get_Double(x)));
return DBL2NUM(exp(num2dbl_with_to_f(x)));
}
#if defined __CYGWIN__
......
numbits = 0;
}
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < 0.0) domain_error("log");
/* check for pole error */
......
numbits = 0;
}
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < 0.0) domain_error("log2");
/* check for pole error */
......
numbits = 0;
}
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < 0.0) domain_error("log10");
/* check for pole error */
......
{
double d0, d;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (d0 < 0.0) domain_error("sqrt");
if (d0 == 0.0) return DBL2NUM(0.0);
......
static VALUE
math_cbrt(VALUE obj, VALUE x)
{
return DBL2NUM(cbrt(Get_Double(x)));
return DBL2NUM(cbrt(num2dbl_with_to_f(x)));
}
/*
......
double d;
int exp;
d = frexp(Get_Double(x), &exp);
d = frexp(num2dbl_with_to_f(x), &exp);
return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
}
......
static VALUE
math_ldexp(VALUE obj, VALUE x, VALUE n)
{
return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
return DBL2NUM(ldexp(num2dbl_with_to_f(x), NUM2INT(n)));
}
/*
......
static VALUE
math_hypot(VALUE obj, VALUE x, VALUE y)
{
return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
return DBL2NUM(hypot(num2dbl_with_to_f(x), num2dbl_with_to_f(y)));
}
/*
......
static VALUE
math_erf(VALUE obj, VALUE x)
{
return DBL2NUM(erf(Get_Double(x)));
return DBL2NUM(erf(num2dbl_with_to_f(x)));
}
/*
......
static VALUE
math_erfc(VALUE obj, VALUE x)
{
return DBL2NUM(erfc(Get_Double(x)));
return DBL2NUM(erfc(num2dbl_with_to_f(x)));
}
/*
......
};
double d0, d;
double intpart, fracpart;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (isinf(d0) && signbit(d0)) domain_error("gamma");
fracpart = modf(d0, &intpart);
......
double d0, d;
int sign=1;
VALUE v;
d0 = Get_Double(x);
d0 = num2dbl_with_to_f(x);
/* check for domain error */
if (isinf(d0)) {
if (signbit(d0)) domain_error("lgamma");
-
(1-1/2)