From 7a6ebecf9ea5d00ccde2d78d45d35aaff3866845 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 4 May 2014 01:23:01 +0000 Subject: math.c: C99-like atan2 * math.c (math_atan2): return values like as expected by C99 if both two arguments are infinity. based on the patch by cremno phobia in [ruby-core:62310]. [Feature #9799] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- math.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'math.c') diff --git a/math.c b/math.c index 70cd26cb07..b042e7dbea 100644 --- a/math.c +++ b/math.c @@ -55,6 +55,10 @@ VALUE rb_eMathDomainError; * Math.atan2(1.0, 0.0) #=> 1.5707963267948966 * Math.atan2(1.0, -1.0) #=> 2.356194490192345 * Math.atan2(0.0, -1.0) #=> 3.141592653589793 + * Math.atan2(INFINITY, INFINITY) #=> 0.7853981633974483 + * Math.atan2(INFINITY, -INFINITY) #=> 2.356194490192345 + * Math.atan2(-INFINITY, INFINITY) #=> -0.7853981633974483 + * Math.atan2(-INFINITY, -INFINITY) #=> -2.356194490192345 * */ @@ -75,7 +79,20 @@ 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 !(defined(HAVE_ATAN2L) && defined(HAVE_ATAN2F)) || 1 + /* assume atan2() doesn't handle Inf as C99 */ + if (isinf(dx) && isinf(dy)) { + /* optimization for FLONUM */ + if (dx < 0.0) { + const double dz = (3.0 * M_PI / 4.0); + return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz); + } + else { + const double dz = (M_PI / 4.0); + return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz); + } + } +#endif return DBL2NUM(atan2(dy, dx)); } -- cgit v1.2.3