Skip to content

Commit

Permalink
* math.c (domain_check): should not raise EDOM exception for NaN
Browse files Browse the repository at this point in the history
  value if operand is also NaN.  affects acos, asin, acosh, atanh,
  log, log2, log10, sqrt, gamma, lgamma. [ruby-dev:37357]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Dec 10, 2008
1 parent 3266ec1 commit 54a722b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Thu Dec 11 02:37:22 2008 Yukihiro Matsumoto <[email protected]>

* math.c (domain_check): should not raise EDOM exception for NaN
value if operand is also NaN. affects acos, asin, acosh, atanh,
log, log2, log10, sqrt, gamma, lgamma. [ruby-dev:37357]

Thu Dec 11 02:23:51 2008 Tanaka Akira <[email protected]>

* lib/open3.rb (Open3.pipeline_start): return an array of threads if a
Expand Down
75 changes: 43 additions & 32 deletions math.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ to_flo(VALUE x)
} while (0)

static void
domain_check(double x, const char *msg)
domain_check(double x, double y, const char *msg)
{
while(1) {
if (errno) {
rb_sys_fail(msg);
}
if (isnan(x)) {
if (isnan(y)) {
if (isnan(x)) break;
#if defined(EDOM)
errno = EDOM;
#elif defined(ERANGE)
Expand Down Expand Up @@ -146,12 +147,13 @@ math_tan(VALUE obj, VALUE x)
static VALUE
math_acos(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = acos(RFLOAT_VALUE(x));
domain_check(d, "acos");
d0 = RFLOAT_VALUE(x);
d = acos(d0);
domain_check(d0, d, "acos");
return DBL2NUM(d);
}

Expand All @@ -165,12 +167,13 @@ math_acos(VALUE obj, VALUE x)
static VALUE
math_asin(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = asin(RFLOAT_VALUE(x));
domain_check(d, "asin");
d0 = RFLOAT_VALUE(x);
d = asin(d0);
domain_check(d0, d, "asin");
return DBL2NUM(d);
}

Expand Down Expand Up @@ -267,12 +270,13 @@ math_tanh(VALUE obj, VALUE x)
static VALUE
math_acosh(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = acosh(RFLOAT_VALUE(x));
domain_check(d, "acosh");
d0 = RFLOAT_VALUE(x);
d = acosh(d0);
domain_check(d0, d, "acosh");
return DBL2NUM(d);
}

Expand Down Expand Up @@ -300,12 +304,13 @@ math_asinh(VALUE obj, VALUE x)
static VALUE
math_atanh(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = atanh(RFLOAT_VALUE(x));
domain_check(d, "atanh");
d0 = RFLOAT_VALUE(x);
d = atanh(d0);
domain_check(d0, d, "atanh");
infinity_check(x, d, "atanh");
return DBL2NUM(d);
}
Expand Down Expand Up @@ -347,17 +352,18 @@ static VALUE
math_log(int argc, VALUE *argv)
{
VALUE x, base;
double d;
double d0, d;

rb_scan_args(argc, argv, "11", &x, &base);
Need_Float(x);
errno = 0;
d = log(RFLOAT_VALUE(x));
d0 = RFLOAT_VALUE(x);
d = log(d0);
if (argc == 2) {
Need_Float(base);
d /= log(RFLOAT_VALUE(base));
}
domain_check(d, "log");
domain_check(d0, d, "log");
infinity_check(x, d, "log");
return DBL2NUM(d);
}
Expand All @@ -384,12 +390,13 @@ extern double log2(double);
static VALUE
math_log2(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = log2(RFLOAT_VALUE(x));
domain_check(d, "log2");
d0 = RFLOAT_VALUE(x);
d = log2(d0);
domain_check(d0, d, "log2");
infinity_check(x, d, "log2");
return DBL2NUM(d);
}
Expand All @@ -404,12 +411,13 @@ math_log2(VALUE obj, VALUE x)
static VALUE
math_log10(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = log10(RFLOAT_VALUE(x));
domain_check(d, "log10");
d0 = RFLOAT_VALUE(x);
d = log10(d0);
domain_check(d0, d, "log10");
infinity_check(x, d, "log10");
return DBL2NUM(d);
}
Expand Down Expand Up @@ -441,12 +449,13 @@ math_log10(VALUE obj, VALUE x)
static VALUE
math_sqrt(VALUE obj, VALUE x)
{
double d;
double d0, d;

Need_Float(x);
errno = 0;
d = sqrt(RFLOAT_VALUE(x));
domain_check(d, "sqrt");
d0 = RFLOAT_VALUE(x);
d = sqrt(d0);
domain_check(d0, d, "sqrt");
return DBL2NUM(d);
}

Expand Down Expand Up @@ -619,11 +628,12 @@ math_erfc(VALUE obj, VALUE x)
static VALUE
math_gamma(VALUE obj, VALUE x)
{
double d;
double d0, d;
Need_Float(x);
errno = 0;
d = tgamma(RFLOAT_VALUE(x));
domain_check(d, "gamma");
d0 = RFLOAT_VALUE(x);
d = tgamma(d0);
domain_check(d0, d, "gamma");
return DBL2NUM(d);
}

Expand All @@ -642,13 +652,14 @@ math_gamma(VALUE obj, VALUE x)
static VALUE
math_lgamma(VALUE obj, VALUE x)
{
double d;
double d0, d;
int sign;
VALUE v;
Need_Float(x);
errno = 0;
d = lgamma_r(RFLOAT_VALUE(x), &sign);
domain_check(d, "lgamma");
d0 = RFLOAT_VALUE(x);
d = lgamma_r(d0, &sign);
domain_check(d0, d, "lgamma");
v = DBL2NUM(d);
return rb_assoc_new(v, INT2FIX(sign));
}
Expand Down

0 comments on commit 54a722b

Please sign in to comment.