Method: Math#atan2

Defined in:
math.c

#atan2(y, x) ⇒ Float (private)

Returns the arc tangent of y and x in radians.

  • Domain of y: [-INFINITY, INFINITY].

  • Domain of x: [-INFINITY, INFINITY].

  • Range: [-PI, PI].

Examples:

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

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'math.c', line 61

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));
}