Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 921
LocalHost Avatar asked Dec 20 '17 15:12

LocalHost


People also ask

What does atan2 return?

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.

How do you calculate atan2?

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.

What is the range of atan2?

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.

What does atan2 mean in C++?

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


1 Answers

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.

like image 62
chux - Reinstate Monica Avatar answered Oct 19 '22 19:10

chux - Reinstate Monica