From 9be503f332515719475216e7788e6a7fab6c78d0 Mon Sep 17 00:00:00 2001 From: mrkn Date: Fri, 27 Aug 2010 03:51:56 +0000 Subject: * math.c (math_atan2): change the behavior when x and y are zero. * test/ruby/test_math.rb (test_atan2): add tests for the above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29115 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ math.c | 8 +++++++- test/ruby/test_math.rb | 5 ++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index fa47ae5882..88f0372014 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Aug 27 12:47:44 2010 Kenta Murata + + * math.c (math_atan2): change the behavior when x and y are zero. + + * test/ruby/test_math.rb (test_atan2): add tests for the above + changes. + Fri Aug 27 12:26:23 2010 NAKAMURA Usaku * object.c (rb_obj_class): remove mention of obsolete method. diff --git a/math.c b/math.c index 23bca55c4b..5461763d56 100644 --- a/math.c +++ b/math.c @@ -55,7 +55,13 @@ math_atan2(VALUE obj, VALUE y, VALUE x) Need_Float2(y, x); dx = RFLOAT_VALUE(x); dy = RFLOAT_VALUE(y); - if (dx == 0.0 && dy == 0.0) domain_error("atan2"); + if (dx == 0.0 && dy == 0.0) { + if (!signbit(dx)) + return DBL2NUM(dy); + if (!signbit(dy)) + return DBL2NUM(M_PI); + return DBL2NUM(-M_PI); + } if (isinf(dx) && isinf(dy)) domain_error("atan2"); return DBL2NUM(atan2(dy, dx)); } diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index 64b72ce5c0..3895eec7f5 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -17,7 +17,10 @@ class TestMath < Test::Unit::TestCase end def test_atan2 - assert_raise(Math::DomainError) { Math.atan2(0, 0) } + check(+0.0, Math.atan2(+0.0, +0.0)) + 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) } -- cgit v1.2.3