cmsMath.c revision 1ab64890
11ab64890Smrg/* $Xorg: cmsMath.c,v 1.4 2001/02/09 02:03:39 xorgcvs Exp $ */
21ab64890Smrg
31ab64890Smrg/*
41ab64890Smrg
51ab64890SmrgCopyright 1990, 1998  The Open Group
61ab64890Smrg
71ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
81ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
91ab64890Smrgthe above copyright notice appear in all copies and that both that
101ab64890Smrgcopyright notice and this permission notice appear in supporting
111ab64890Smrgdocumentation.
121ab64890Smrg
131ab64890SmrgThe above copyright notice and this permission notice shall be included in
141ab64890Smrgall copies or substantial portions of the Software.
151ab64890Smrg
161ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
191ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
201ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
211ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
221ab64890Smrg
231ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
241ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
251ab64890Smrgin this Software without prior written authorization from The Open Group.
261ab64890Smrg
271ab64890Smrg*/
281ab64890Smrg/* $XFree86: xc/lib/X11/cmsMath.c,v 3.4 2001/07/29 05:01:11 tsi Exp $ */
291ab64890Smrg
301ab64890Smrg/*
311ab64890Smrg * Stephen Gildea, MIT X Consortium, January 1991
321ab64890Smrg */
331ab64890Smrg
341ab64890Smrg#ifdef HAVE_CONFIG_H
351ab64890Smrg#include <config.h>
361ab64890Smrg#endif
371ab64890Smrg#include "Xlibint.h"
381ab64890Smrg#include "Xcmsint.h"
391ab64890Smrg
401ab64890Smrg#include <float.h>
411ab64890Smrg#ifndef DBL_EPSILON
421ab64890Smrg#define DBL_EPSILON 1e-6
431ab64890Smrg#endif
441ab64890Smrg
451ab64890Smrg#ifdef _X_ROOT_STATS
461ab64890Smrgint cbrt_loopcount;
471ab64890Smrgint sqrt_loopcount;
481ab64890Smrg#endif
491ab64890Smrg
501ab64890Smrg/* Newton's Method:  x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
511ab64890Smrg
521ab64890Smrg
531ab64890Smrg/* for cube roots, x^3 - a = 0,  x_new = x - 1/3 (x - a/x^2) */
541ab64890Smrg
551ab64890Smrgdouble
561ab64890Smrg_XcmsCubeRoot(double a)
571ab64890Smrg{
581ab64890Smrg    register double abs_a, cur_guess, delta;
591ab64890Smrg
601ab64890Smrg#ifdef DEBUG
611ab64890Smrg    printf("_XcmsCubeRoot passed in %g\n", a);
621ab64890Smrg#endif
631ab64890Smrg#ifdef _X_ROOT_STATS
641ab64890Smrg    cbrt_loopcount = 0;
651ab64890Smrg#endif
661ab64890Smrg    if (a == 0.)
671ab64890Smrg	return 0.;
681ab64890Smrg
691ab64890Smrg    abs_a = a<0. ? -a : a;	/* convert to positive to speed loop tests */
701ab64890Smrg
711ab64890Smrg    /* arbitrary first guess */
721ab64890Smrg    if (abs_a > 1.)
731ab64890Smrg	cur_guess = abs_a/8.;
741ab64890Smrg    else
751ab64890Smrg	cur_guess = abs_a*8.;
761ab64890Smrg
771ab64890Smrg    do {
781ab64890Smrg#ifdef _X_ROOT_STATS
791ab64890Smrg	cbrt_loopcount++;
801ab64890Smrg#endif
811ab64890Smrg	delta = (cur_guess - abs_a/(cur_guess*cur_guess))/3.;
821ab64890Smrg	cur_guess -= delta;
831ab64890Smrg	if (delta < 0.) delta = -delta;
841ab64890Smrg    } while (delta >= cur_guess*DBL_EPSILON);
851ab64890Smrg
861ab64890Smrg    if (a < 0.)
871ab64890Smrg	cur_guess = -cur_guess;
881ab64890Smrg
891ab64890Smrg#ifdef DEBUG
901ab64890Smrg    printf("_XcmsCubeRoot returning %g\n", cur_guess);
911ab64890Smrg#endif
921ab64890Smrg    return cur_guess;
931ab64890Smrg}
941ab64890Smrg
951ab64890Smrg
961ab64890Smrg
971ab64890Smrg/* for square roots, x^2 - a = 0,  x_new = x - 1/2 (x - a/x) */
981ab64890Smrg
991ab64890Smrgdouble
1001ab64890Smrg_XcmsSquareRoot(double a)
1011ab64890Smrg{
1021ab64890Smrg    register double cur_guess, delta;
1031ab64890Smrg
1041ab64890Smrg#ifdef DEBUG
1051ab64890Smrg    printf("_XcmsSquareRoot passed in %g\n", a);
1061ab64890Smrg#endif
1071ab64890Smrg#ifdef _X_ROOT_STATS
1081ab64890Smrg    sqrt_loopcount = 0;
1091ab64890Smrg#endif
1101ab64890Smrg    if (a == 0.)
1111ab64890Smrg	return 0.;
1121ab64890Smrg
1131ab64890Smrg    if (a < 0.) {
1141ab64890Smrg	/* errno = EDOM; */
1151ab64890Smrg	return 0.;
1161ab64890Smrg    }
1171ab64890Smrg
1181ab64890Smrg    /* arbitrary first guess */
1191ab64890Smrg    if (a > 1.)
1201ab64890Smrg	cur_guess = a/4.;
1211ab64890Smrg    else
1221ab64890Smrg	cur_guess = a*4.;
1231ab64890Smrg
1241ab64890Smrg    do {
1251ab64890Smrg#ifdef _X_ROOT_STATS
1261ab64890Smrg	sqrt_loopcount++;
1271ab64890Smrg#endif
1281ab64890Smrg	delta = (cur_guess - a/cur_guess)/2.;
1291ab64890Smrg	cur_guess -= delta;
1301ab64890Smrg	if (delta < 0.) delta = -delta;
1311ab64890Smrg    } while (delta >= cur_guess*DBL_EPSILON);
1321ab64890Smrg
1331ab64890Smrg#ifdef DEBUG
1341ab64890Smrg    printf("_XcmsSquareRoot returning %g\n", cur_guess);
1351ab64890Smrg#endif
1361ab64890Smrg    return cur_guess;
1371ab64890Smrg}
1381ab64890Smrg
139