cundefinedtrigonometryangleatan2

what will be atan2 output for both x and y as 0


theta=atan2(0,0);

The output of this statement is 0, but it is a 0/0 form , so how its output can be 0, even wikipedia says that it should be undefined , please explain why compiler gives 0 as output for this statement.


Solution

  • what will be atan2 output for both x and y as 0

    C has 2 relevant specifications. @Oliver Charlesworth @Jonathan Leffler

    The atan2 functions ... A domain error may occur if both arguments are zero. C11dr §7.12.4.4 2

    Treatment of error conditions ...For all functions, a domain error occurs if an input argument is outside the domain over which the mathematical function is defined. ...; On a domain error, the function returns an implementation-defined value;... §7.12.1 3

    atan2(0,0) may lead to 0.0, 1.0, INF, NAN, etc. It is not specified other than something is returned.

    0.0 is certainly a reasonable choice yet there is no clear mathematically correct result - it is not defined.


    For compatibility with IEC-60559 (which is not required by C although many implementation strive to adhere), the following results are mandated. Note the differences due to ±0.

    atan2(±0, −0) returns ±π
    atan2(±0, +0) returns ±0.
    

    A note on π. π being an irrational number and all finite double are rational, a return value of machine pi (the closest representable double to π) is expected with atan2(+0, -0).


    Shorter version

    "what will be atan2 output for both x and y as 0"

    Zero.

    "how its output can be 0, even wikipedia says that it should be undefined"

    Wikipedia does not define C nor various math library specifications - it is a reference - not a specification.

    "why compiler gives 0"

    That is the specified response from the floating point standard IEC-60559/IEEE 754 often used by various C implementations.