5
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.

LocalHost
  • 910
  • 3
  • 8
  • 24
  • 5
    The C standard says that `atan2(0, 0)` is a *domain error*, which leads to an implementation-defined return value. – Oliver Charlesworth Dec 20 '17 at 15:11
  • 1
    Strictly, the standard says "A domain error may occur if both arguments are zero." The net result is much the same. The standard also details what is supposed to happen in section §7.12.1 Treatment of Error Conditions. – Jonathan Leffler Dec 20 '17 at 15:18
  • 1
    Are you asking what kind of behaviour to expect when the behaviour is undefined..? – underscore_d Dec 20 '17 at 15:26

1 Answers1

14

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256