# Re: sin(x) for large x

Discussion in 'C Programming' started by glen herrmannsfeldt, May 18, 2004.

1. ### glen herrmannsfeldtGuest

James Giles wrote:

(someone wrote)

>>I submit, that if a change in the least significant bit of the
>>argument (which is very frequently but a finite-precision rational
>>approximation to a real in itself) is larger than pi, the best thing
>>to return is 'NaN', and most of the time the users should be trapping
>>on NaN's.

In the days before NaN's it could have just been a fatal
error. In one IBM manual on the DSIN function, (comparable
to the C sin function)...

They limit the domain to +/- PI*2**50, which I believe leaves
four bits after the binary point (or one digit after the
hexadecimal point for purists). The then quote relative and
absolute accuracy figures as follows:

Relative Absolute
Max Std.Dev. Max. Std.Dev.

abs(X)<= PI/2 3.60e-16 4.82e-17 7.74e-17 1.98e-17
pi/2<abs(X)<=10 1.64e-16 6.49e-17
10<abs(X)<=100 2.68e-15 1.03e-15

So, in the range where they expect most users to use
the function they give reasonably detailed numbers. Do any
of the libraries more modern than this, from other companies,
give this information?

They also give details on the argument reduction used:

Divide abs(x) by pi/4 and separate into integer part (q),
and fraction part (r). 0<=r<1.

If cosine is desired, add 2 to q. If sine is desired and
x is negative add 4 to q.

Using q0= q mod 8,

for q0=0 use sin((pi/4)*r)
q0=1 cos((pi/4)*(1-r))
q0=2 cos((pi/4)*r)
q0=3 sin((pi/4)*(1-r))
q0=4 -sin((pi/4)*r)
q0=5 -cos((pi/4)*(1-r))
q0=6 -cos((pi/4)*r)
q0=7 -sin((pi/4)*(1-r))

Using polynomial interpolation of degree 6 in r**2 for sin,
or degree 7 in r**2 for cosine. In either case, the interpolation
points were the roots of the Chebyshev polynomial of one higher
degree. The maximum relative error for the sin polynomial
is 2**-58, of the cosine polynomial is 2**-64.3.

Not that all the details are important here, but the amount
of detail that went into the explanation. This is from
the Application Programming: Library Reference, meant to

> Well, that argument would have more support if we were
> using intervals, or significance arithmetic, or some other
> means to denote the accuracy to which values were known.
> Then the transition from significant to NaN wouldn't be just
> a sudden jump. The answers would be larger and larger
> intervals (or fewer and fewer digits) until the whole interval
> [-1.0, 1.0] was returned (or, "no significance"). Without that,
> where are you going to draw the line?

In the case above, no accuracy numbers were given past 100,
and one can see how fast the accuracy decays at that point.

> NaN would be a good representation for "no significance". But
> how do you propose telling the naive user that there was some,
> but not much, significance? And, within ordinary ranges, it's not
> entirely foolish to want a full precision answer (so, whatever
> your solution is, you need to allow real experts to turn it off).

Real experts can do their own range reduction in the way
appropriate to the problem they are working on.

-- glen

glen herrmannsfeldt, May 18, 2004