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