summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2023-07-17 23:57:11 +0900
committerNobuyoshi Nakada <[email protected]>2023-07-17 23:57:11 +0900
commit105bdba899fbb10aa51115c4cd074ea42eb9e3e6 (patch)
tree224d413b73d209dbf42e29be81ffaa6ea2a95123
parentd70484f0eb176a7ef7274972ff92b88905ea0edc (diff)
Fix logarithm of 0 with base
-rw-r--r--math.c5
-rw-r--r--test/ruby/test_math.rb9
2 files changed, 8 insertions, 6 deletions
diff --git a/math.c b/math.c
index 1e2af7aa75..6f0ddf9ec5 100644
--- a/math.c
+++ b/math.c
@@ -554,9 +554,8 @@ rb_math_log(int argc, const VALUE *argv)
double b = math_log_split(base, &numbits_2);
/* check for pole error */
if (d == 0.0) {
- if (b > 0.0) return DBL2NUM(HUGE_VAL);
- if (b < 0.0) return DBL2NUM(-HUGE_VAL);
- return DBL2NUM(nan(""));
+ // Already DomainError if b < 0.0
+ return b ? DBL2NUM(-HUGE_VAL) : DBL2NUM(NAN);
}
else if (b == 0.0) {
return DBL2NUM(-0.0);
diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb
index 73f44c6ae3..bc2172d680 100644
--- a/test/ruby/test_math.rb
+++ b/test/ruby/test_math.rb
@@ -5,6 +5,7 @@ class TestMath < Test::Unit::TestCase
def assert_infinity(a, *rest)
rest = ["not infinity: #{a.inspect}"] if rest.empty?
assert_predicate(a, :infinite?, *rest)
+ assert_predicate(a, :positive?, *rest)
end
def assert_nan(a, *rest)
@@ -165,6 +166,8 @@ class TestMath < Test::Unit::TestCase
assert_nothing_raised { assert_nan(Math.log(0.0, 0.0)) }
assert_nothing_raised { assert_nan(Math.log(Float::NAN)) }
assert_nothing_raised { assert_nan(Math.log(1.0, Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log(0)) }
+ assert_nothing_raised { assert_infinity(-Math.log(0, 2)) }
end
def test_log2
@@ -179,6 +182,7 @@ class TestMath < Test::Unit::TestCase
assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-1.0) }
assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-Float::EPSILON) }
assert_nothing_raised { assert_nan(Math.log2(Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log2(0)) }
end
def test_log10
@@ -193,6 +197,7 @@ class TestMath < Test::Unit::TestCase
assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-1.0) }
assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-Float::EPSILON) }
assert_nothing_raised { assert_nan(Math.log10(Float::NAN)) }
+ assert_nothing_raised { assert_infinity(-Math.log10(0)) }
end
def test_sqrt
@@ -277,8 +282,7 @@ class TestMath < Test::Unit::TestCase
assert_raise_with_message(Math::DomainError, /\bgamma\b/) { Math.gamma(-1.0) }
x = Math.gamma(-0.0)
mesg = "Math.gamma(-0.0) should be -INF"
- assert_infinity(x, mesg)
- assert_predicate(x, :negative?, mesg)
+ assert_infinity(-x, mesg)
assert_nan(Math.gamma(Float::NAN))
end
@@ -299,7 +303,6 @@ class TestMath < Test::Unit::TestCase
x, sign = Math.lgamma(-0.0)
mesg = "Math.lgamma(-0.0) should be [INF, -1]"
assert_infinity(x, mesg)
- assert_predicate(x, :positive?, mesg)
assert_equal(-1, sign, mesg)
x, sign = Math.lgamma(Float::NAN)
assert_nan(x)