Mark McIntyre wrote:

>

> On Sat, 15 Oct 2005 13:07:20 GMT, in comp.lang.c , pete

> <(E-Mail Removed)> wrote:

>

> >pete wrote:

> >

> >> .. and now for a float sqrt algorithm:

> >

> >I just wrote a cosine function called c_os, for no special reason.

> >It calls no functions that I haven't also written.

> >

> >On my machine, it seems to work better than

> >my MS compiler's standard library cos function.

> >They don't get the zeros right. I do.

>

> Its equally possible that you're getting the cos of a number close to

> n pi/2 wrong....

> Its even possible, (I've not read your algo), that you have

> special-case handling.
There's just a smidgeon of special case handling in sq_rt.

In these cases:

c_os(-123 * pi / 6) + 0.0 is 0.000000e+000

c_os(-129 * pi / 6) + 0.0 is 0.000000e+000

cos(-123 * pi / 6) + 0.0 is 7.839514e-015

cos(-129 * pi / 6) + 0.0 is -4.409261e-015

For values of x, which are equal to the product

of an odd integer and c_os's approximation of pi / 2,

the c_os function, normalizes x all the way down to 0.0,

and calculates the sine of 0.0 from the cosine.

c_os then reports the sine of 0.0, as the cosine of pi / 2.

The special case is that that involves using sq_rt

to find the root of zero. sq_rt(0) is a special case.

When I was writing c_os, it didn't originally

normalize x any lower than pi / 2,

but I found that I was having accuaracy problems all around

the vicinity of pi / 2.

The infinite series that c_os approximates,

converges rapidly for small x,

but not rapidly when x is greater than 1.

The two generally recognized sources of error

when summing a series, are:

1 the error in each term

2 the error from only summing a finite number of terms.

A more rapidly converging series, reduces both errors.

I was wondering if my implementation's cos function

doesn't normalized x, when x is about pi / 2.

c_os agrees with cos on my system for some input.

c_os(-120 * pi / 6) - 1.0 is 0.000000e+000

c_os(-126 * pi / 6) + 1.0 is 0.000000e+000

cos(-120 * pi / 6) - 1.0 is 0.000000e+000

cos(-126 * pi / 6) + 1.0 is 0.000000e+000

I'm am suspecting that it's because the series summing do loop,

converges after just one iteration when x == 0.0,

and also because c_os and cos are using a value for pi

which is effectively the same as each other's.

--

pete