Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to assume that atan2(0,0) returns 0?

Tags:

python

c

atan2

It seems thatatan2(0,0) in C is defined this way. Python just delegates the common trigonometric functions to the underlying C implementation, right?

like image 497
Emanuel Landeholm Avatar asked Mar 04 '23 09:03

Emanuel Landeholm


1 Answers

The Python docs don't explicitly specify atan2(0, 0), but the CPython implementation goes to specific effort to ensure special cases follow C99 even if the underlying C library doesn't. This includes a code path that handles making atan2(0, 0) return 0.0:

if (Py_IS_INFINITY(x) || y == 0.) {
    if (copysign(1., x) == 1.)
        /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
        return copysign(0., y);
    ...

In general, functions provided in the math module that correspond to C standard library functions are expected to match the C standard behavior. There are a few blanket statements to this effect, such as this one near the top of the docs:

This module is always available. It provides access to the mathematical functions defined by the C standard.

and this one near the bottom:

Behavior in exceptional cases follows Annex F of the C99 standard where appropriate.

although Annex F isn't the part of the standard that defines atan2.

like image 96
user2357112 supports Monica Avatar answered Mar 27 '23 21:03

user2357112 supports Monica