Skip to content

Commit

Permalink
* math.c (domain_check): a new function to check domain error
Browse files Browse the repository at this point in the history
  explicitly for systems that return NaN like FreeBSD.
  [ruby-core:07019]

* math.c (math_acos, math_asin, math_acosh, math_atanh, math_log,
  math_log10, math_sqrt): use domain_check().

* math.c (math_sqrt): fix documentation flaw.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10626 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Jul 27, 2006
1 parent 41005d4 commit 21f5f47
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Thu Jul 27 21:19:54 2006 Yukihiro Matsumoto <[email protected]>

* math.c (domain_check): a new function to check domain error
explicitly for systems that return NaN like FreeBSD.
[ruby-core:07019]

* math.c (math_acos, math_asin, math_acosh, math_atanh, math_log,
math_log10, math_sqrt): use domain_check().

* math.c (math_sqrt): fix documentation flaw.

Thu Jul 27 22:21:52 2006 Nobuyoshi Nakada <[email protected]>

* time.c (time_to_s): fixed format mismatch.
Expand Down
56 changes: 29 additions & 27 deletions math.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ VALUE rb_mMath;
Need_Float(y);\
} while (0)

static void
domain_check(x, msg)
double x;
char *msg;
{
while(1) {
if (errno) {
rb_sys_fail(msg);
}
if (isnan(x)) {
#if defined(EDOM)
errno = EDOM;
#elif define(ERANGE)
errno = ERANGE;
#endif
continue;
}
break;
}
}


/*
* call-seq:
Expand All @@ -40,7 +61,6 @@ math_atan2(VALUE obj, VALUE y, VALUE x)
}



/*
* call-seq:
* Math.cos(x) => float
Expand Down Expand Up @@ -103,9 +123,7 @@ math_acos(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = acos(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("acos");
}
domain_check(d, "acos");
return rb_float_new(d);
}

Expand All @@ -124,9 +142,7 @@ math_asin(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = asin(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("asin");
}
domain_check(d, "asin");
return rb_float_new(d);
}

Expand Down Expand Up @@ -228,9 +244,7 @@ math_acosh(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = acosh(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("acosh");
}
domain_check(d, "acosh");
return rb_float_new(d);
}

Expand Down Expand Up @@ -263,9 +277,7 @@ math_atanh(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = atanh(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("atanh");
}
domain_check(d, "atanh");
return rb_float_new(d);
}

Expand Down Expand Up @@ -309,16 +321,11 @@ math_log(int argc, VALUE *argv)
Need_Float(x);
errno = 0;
d = log(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("log");
}
if (!NIL_P(base)) {
Need_Float(base);
d /= log(RFLOAT(base)->value);
}
if (errno) {
rb_sys_fail("log");
}
domain_check(d);
return rb_float_new(d);
}

Expand Down Expand Up @@ -370,18 +377,15 @@ math_log10(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = log10(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("log10");
}
domain_check(d, "log10");
return rb_float_new(d);
}

/*
* call-seq:
* Math.sqrt(numeric) => float
*
* Returns the non-negative square root of <i>numeric</i>. Raises
* <code>ArgError</code> if <i>numeric</i> is less than zero.
* Returns the non-negative square root of <i>numeric</i>.
*/

static VALUE
Expand All @@ -392,9 +396,7 @@ math_sqrt(VALUE obj, VALUE x)
Need_Float(x);
errno = 0;
d = sqrt(RFLOAT(x)->value);
if (errno) {
rb_sys_fail("sqrt");
}
domain_check(d, "sqrt");
return rb_float_new(d);
}

Expand Down

0 comments on commit 21f5f47

Please sign in to comment.