summaryrefslogtreecommitdiff
path: root/math.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-03 05:28:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-03 05:28:46 +0000
commit38656e24bea4a6ced1ebc18a0178bf8c1b175ca7 (patch)
tree34a3a3c204a7c50246c3f83fba378bc4f590c00f /math.c
parent1fd4c7f1f5efff77579172b7ca7ae7e971361919 (diff)
math.c: check domain of base argument
* math.c (math_log): check domain of base argument too. raises Math::DomainError instead of returning NaN if the base is less than 0, and returns NaN instead of -infinity if both of two arguments are 0. [ruby-core:62309] [Bug #9797] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'math.c')
-rw-r--r--math.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/math.c b/math.c
index 8a5e5cdc33..70cd26cb07 100644
--- a/math.c
+++ b/math.c
@@ -416,6 +416,8 @@ math_exp(VALUE obj, VALUE x)
# define log10(x) ((x) < 0.0 ? nan("") : log10(x))
#endif
+static double math_log1(VALUE x);
+
/*
* call-seq:
* Math.log(x) -> Float
@@ -441,10 +443,21 @@ static VALUE
math_log(int argc, VALUE *argv, VALUE obj)
{
VALUE x, base;
- double d0, d;
- size_t numbits;
+ double d;
rb_scan_args(argc, argv, "11", &x, &base);
+ d = math_log1(x);
+ if (argc == 2) {
+ d /= math_log1(base);
+ }
+ return DBL2NUM(d);
+}
+
+static double
+math_log1(VALUE x)
+{
+ double d0, d;
+ size_t numbits;
if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
@@ -460,15 +473,11 @@ math_log(int argc, VALUE *argv, VALUE obj)
/* check for domain error */
if (d0 < 0.0) domain_error("log");
/* check for pole error */
- if (d0 == 0.0) return DBL2NUM(-INFINITY);
+ if (d0 == 0.0) return -INFINITY;
d = log(d0);
if (numbits)
d += numbits * log(2); /* log(2**numbits) */
- if (argc == 2) {
- Need_Float(base);
- d /= log(RFLOAT_VALUE(base));
- }
- return DBL2NUM(d);
+ return d;
}
#ifndef log2