Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Math.atan2
Math.atan2(y, x) → float
math.c
View on GitHub ↗
static VALUE
math_atan2(VALUE unused_obj, VALUE y, VALUE x)
{
double dx, dy;
dx = Get_Double(x);
dy = Get_Double(y);
if (dx == 0.0 && dy == 0.0) {
if (!signbit(dx))
return DBL2NUM(dy);
if (!signbit(dy))
return DBL2NUM(M_PI);
return DBL2NUM(-M_PI);
}
#ifndef ATAN2_INF_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));
}
Returns the arc tangent of y and x in radians.
-
Domain of
y:[-INFINITY, INFINITY]. -
Domain of
x:[-INFINITY, INFINITY]. -
Range:
[-PI, PI].
Examples:
Example
atan2(-1.0, -1.0) # => -2.356194490192345 # -3*PI/4 atan2(-1.0, 0.0) # => -1.5707963267948966 # -PI/2 atan2(-1.0, 1.0) # => -0.7853981633974483 # -PI/4 atan2(0.0, -1.0) # => 3.141592653589793 # PI