diff --git a/math.c b/math.c index 898ccb6..fd109eb 100644 --- a/math.c +++ b/math.c @@ -75,7 +75,11 @@ math_atan2(VALUE obj, VALUE y, VALUE x) return DBL2NUM(M_PI); return DBL2NUM(-M_PI); } - if (isinf(dx) && isinf(dy)) domain_error("atan2"); + if (isinf(dx) && isinf(dy)) { + double dz = (dx < 0.0) ? (3.0 * M_PI / 4.0) : (M_PI / 4.0); + if (dy < 0.0) dz = -dz; + return DBL2NUM(dz); + } return DBL2NUM(atan2(dy, dx)); } diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index 74b3940..53630d5 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -21,10 +21,13 @@ def test_atan2 check(-0.0, Math.atan2(-0.0, +0.0)) check(+Math::PI, Math.atan2(+0.0, -0.0)) check(-Math::PI, Math.atan2(-0.0, -0.0)) - assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, Float::INFINITY) } - assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, -Float::INFINITY) } - assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, Float::INFINITY) } - assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, -Float::INFINITY) } + inf = Float::INFINITY + expected = 3.0 * Math::PI / 4.0 + assert_nothing_raised { check(+expected, Math.atan2(+inf, -inf)) } + assert_nothing_raised { check(-expected, Math.atan2(-inf, -inf)) } + expected = Math::PI / 4.0 + assert_nothing_raised { check(+expected, Math.atan2(+inf, +inf)) } + assert_nothing_raised { check(-expected, Math.atan2(-inf, +inf)) } check(0, Math.atan2(0, 1)) check(Math::PI / 4, Math.atan2(1, 1)) check(Math::PI / 2, Math.atan2(1, 0))