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.
atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to the point (x,y), for Math.
The atan2 function calculates one unique arc tangent value from two variables y and x, where the signs of both arguments are used to determine the quadrant of the result, thereby selecting the desired branch of the arc tangent of y/x, e.g., atan2(1, 1) = π/4 and atan2(−1, −1) = −3π/4.
Return Value The atan() function returns a value in the range -π/2 to π/2 radians. The atan2() function returns a value in the range -π to π radians.
C++ atan2() The atan2() function in C++ returns the inverse tangent of a coordinate in radians. It is defined in the cmath header file. Mathematically, atan2(y, x) = tan-1(y/x) .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With