On 11/11/10 12:19 AM, Jarno Rajahalme wrote:
See below the related C standard text (as found from http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm).
Note the paragraph #3, which specifies the range of returned
values. Returning single(pi) using the normal rounding rules is
simply wrong, as the result is outside of the prescibed range,
since single(pi) > pi. Note that it happens that the double
representation of PI happens to round towards zero, so
double(pi) is in the right range.
Returning a value on the wrong side of PI puts the returned
angle to the wrong quadrant of the circle, flipping the sign of
the tan function, for example:
single(pi) > PI:
octave:76> tan(double(single(pi)))
ans = 8.74227800037251e-08
rounding towards zero:
octave:77> tan(double(single(pi-1e-7)))
ans = -1.50995799097839e-07
octave:78>
242 Committee Draft -- August 3, 1998 WG14/N843
7.12.4.4 The atan2 functions
Synopsis
[#1]
#include <math.h>
double atan2(double y, double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);
Description
[#2] The atan2 functions compute the principal value of the
arc tangent of y/x, using the signs of both arguments to
determine the quadrant of the return value. A domain error
may occur if both arguments are zero.
Returns
[#3] The atan2 functions return the arc tangent of y/x, in
the range [-pi, +pi] radians.
OK. I think this is correct, but it is time to do some more
checking
if I can find the time. Anyhow, very good that you looked this up.
Ending up in a wrong quadrant is also bad!
Michael
|