Skip to content

Commit 105bdba

Browse files
committed
Fix logarithm of 0 with base
1 parent d70484f commit 105bdba

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

math.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,8 @@ rb_math_log(int argc, const VALUE *argv)
554554
double b = math_log_split(base, &numbits_2);
555555
/* check for pole error */
556556
if (d == 0.0) {
557-
if (b > 0.0) return DBL2NUM(HUGE_VAL);
558-
if (b < 0.0) return DBL2NUM(-HUGE_VAL);
559-
return DBL2NUM(nan(""));
557+
// Already DomainError if b < 0.0
558+
return b ? DBL2NUM(-HUGE_VAL) : DBL2NUM(NAN);
560559
}
561560
else if (b == 0.0) {
562561
return DBL2NUM(-0.0);

test/ruby/test_math.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TestMath < Test::Unit::TestCase
55
def assert_infinity(a, *rest)
66
rest = ["not infinity: #{a.inspect}"] if rest.empty?
77
assert_predicate(a, :infinite?, *rest)
8+
assert_predicate(a, :positive?, *rest)
89
end
910

1011
def assert_nan(a, *rest)
@@ -165,6 +166,8 @@ def test_log
165166
assert_nothing_raised { assert_nan(Math.log(0.0, 0.0)) }
166167
assert_nothing_raised { assert_nan(Math.log(Float::NAN)) }
167168
assert_nothing_raised { assert_nan(Math.log(1.0, Float::NAN)) }
169+
assert_nothing_raised { assert_infinity(-Math.log(0)) }
170+
assert_nothing_raised { assert_infinity(-Math.log(0, 2)) }
168171
end
169172

170173
def test_log2
@@ -179,6 +182,7 @@ def test_log2
179182
assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-1.0) }
180183
assert_raise_with_message(Math::DomainError, /\blog2\b/) { Math.log2(-Float::EPSILON) }
181184
assert_nothing_raised { assert_nan(Math.log2(Float::NAN)) }
185+
assert_nothing_raised { assert_infinity(-Math.log2(0)) }
182186
end
183187

184188
def test_log10
@@ -193,6 +197,7 @@ def test_log10
193197
assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-1.0) }
194198
assert_raise_with_message(Math::DomainError, /\blog10\b/) { Math.log10(-Float::EPSILON) }
195199
assert_nothing_raised { assert_nan(Math.log10(Float::NAN)) }
200+
assert_nothing_raised { assert_infinity(-Math.log10(0)) }
196201
end
197202

198203
def test_sqrt
@@ -277,8 +282,7 @@ def test_gamma
277282
assert_raise_with_message(Math::DomainError, /\bgamma\b/) { Math.gamma(-1.0) }
278283
x = Math.gamma(-0.0)
279284
mesg = "Math.gamma(-0.0) should be -INF"
280-
assert_infinity(x, mesg)
281-
assert_predicate(x, :negative?, mesg)
285+
assert_infinity(-x, mesg)
282286
assert_nan(Math.gamma(Float::NAN))
283287
end
284288

@@ -299,7 +303,6 @@ def test_lgamma
299303
x, sign = Math.lgamma(-0.0)
300304
mesg = "Math.lgamma(-0.0) should be [INF, -1]"
301305
assert_infinity(x, mesg)
302-
assert_predicate(x, :positive?, mesg)
303306
assert_equal(-1, sign, mesg)
304307
x, sign = Math.lgamma(Float::NAN)
305308
assert_nan(x)

0 commit comments

Comments
 (0)