diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-04 01:23:01 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-05-04 01:23:01 +0000 |
commit | 7a6ebecf9ea5d00ccde2d78d45d35aaff3866845 (patch) | |
tree | e13381d3405b3685d7f1826debf75fe3106bb855 /math.c | |
parent | 6e34259271c705b3d8bb300b5374addb51dce5a0 (diff) | |
download | ruby-7a6ebecf9ea5d00ccde2d78d45d35aaff3866845.tar.gz |
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 <cremno AT mail.ru> in [ruby-core:62310]. [Feature #9799]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'math.c')
-rw-r--r-- | math.c | 19 |
1 files changed, 18 insertions, 1 deletions
@@ -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)); } |