cmsMath.c revision 2f44462d
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 403f3e8497Schristos#ifdef DEBUG 413f3e8497Schristos#include <stdio.h> 423f3e8497Schristos#endif 433f3e8497Schristos 441ab64890Smrg#include <float.h> 451ab64890Smrg#ifndef DBL_EPSILON 461ab64890Smrg#define DBL_EPSILON 1e-6 471ab64890Smrg#endif 481ab64890Smrg 491ab64890Smrg#ifdef _X_ROOT_STATS 501ab64890Smrgint cbrt_loopcount; 511ab64890Smrgint sqrt_loopcount; 521ab64890Smrg#endif 531ab64890Smrg 541ab64890Smrg/* Newton's Method: x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */ 551ab64890Smrg 561ab64890Smrg 571ab64890Smrg/* for cube roots, x^3 - a = 0, x_new = x - 1/3 (x - a/x^2) */ 581ab64890Smrg 591ab64890Smrgdouble 601ab64890Smrg_XcmsCubeRoot(double a) 611ab64890Smrg{ 621ab64890Smrg register double abs_a, cur_guess, delta; 631ab64890Smrg 641ab64890Smrg#ifdef DEBUG 651ab64890Smrg printf("_XcmsCubeRoot passed in %g\n", a); 661ab64890Smrg#endif 671ab64890Smrg#ifdef _X_ROOT_STATS 681ab64890Smrg cbrt_loopcount = 0; 691ab64890Smrg#endif 701ab64890Smrg if (a == 0.) 711ab64890Smrg return 0.; 721ab64890Smrg 731ab64890Smrg abs_a = a<0. ? -a : a; /* convert to positive to speed loop tests */ 741ab64890Smrg 751ab64890Smrg /* arbitrary first guess */ 761ab64890Smrg if (abs_a > 1.) 771ab64890Smrg cur_guess = abs_a/8.; 781ab64890Smrg else 791ab64890Smrg cur_guess = abs_a*8.; 801ab64890Smrg 811ab64890Smrg do { 821ab64890Smrg#ifdef _X_ROOT_STATS 831ab64890Smrg cbrt_loopcount++; 841ab64890Smrg#endif 851ab64890Smrg delta = (cur_guess - abs_a/(cur_guess*cur_guess))/3.; 861ab64890Smrg cur_guess -= delta; 871ab64890Smrg if (delta < 0.) delta = -delta; 881ab64890Smrg } while (delta >= cur_guess*DBL_EPSILON); 891ab64890Smrg 901ab64890Smrg if (a < 0.) 911ab64890Smrg cur_guess = -cur_guess; 921ab64890Smrg 931ab64890Smrg#ifdef DEBUG 941ab64890Smrg printf("_XcmsCubeRoot returning %g\n", cur_guess); 951ab64890Smrg#endif 961ab64890Smrg return cur_guess; 971ab64890Smrg} 982f44462dSmrg 991ab64890Smrg 1001ab64890Smrg 1011ab64890Smrg/* for square roots, x^2 - a = 0, x_new = x - 1/2 (x - a/x) */ 1021ab64890Smrg 1031ab64890Smrgdouble 1041ab64890Smrg_XcmsSquareRoot(double a) 1051ab64890Smrg{ 1061ab64890Smrg register double cur_guess, delta; 1071ab64890Smrg 1081ab64890Smrg#ifdef DEBUG 1091ab64890Smrg printf("_XcmsSquareRoot passed in %g\n", a); 1101ab64890Smrg#endif 1111ab64890Smrg#ifdef _X_ROOT_STATS 1121ab64890Smrg sqrt_loopcount = 0; 1131ab64890Smrg#endif 1141ab64890Smrg if (a == 0.) 1151ab64890Smrg return 0.; 1161ab64890Smrg 1171ab64890Smrg if (a < 0.) { 1181ab64890Smrg /* errno = EDOM; */ 1191ab64890Smrg return 0.; 1201ab64890Smrg } 1211ab64890Smrg 1221ab64890Smrg /* arbitrary first guess */ 1231ab64890Smrg if (a > 1.) 1241ab64890Smrg cur_guess = a/4.; 1251ab64890Smrg else 1261ab64890Smrg cur_guess = a*4.; 1271ab64890Smrg 1281ab64890Smrg do { 1291ab64890Smrg#ifdef _X_ROOT_STATS 1301ab64890Smrg sqrt_loopcount++; 1311ab64890Smrg#endif 1321ab64890Smrg delta = (cur_guess - a/cur_guess)/2.; 1331ab64890Smrg cur_guess -= delta; 1341ab64890Smrg if (delta < 0.) delta = -delta; 1351ab64890Smrg } while (delta >= cur_guess*DBL_EPSILON); 1361ab64890Smrg 1371ab64890Smrg#ifdef DEBUG 1381ab64890Smrg printf("_XcmsSquareRoot returning %g\n", cur_guess); 1391ab64890Smrg#endif 1401ab64890Smrg return cur_guess; 1411ab64890Smrg} 1422f44462dSmrg 143