home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xcms_Math.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  4KB  |  140 lines

  1. /* $XConsortium: cmsMath.c,v 1.11 95/06/08 23:20:39 gildea Exp $ */
  2.  
  3. /*
  4.  
  5. Copyright (c) 1990  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. Except as contained in this notice, the name of the X Consortium shall not be
  25. used in advertising or otherwise to promote the sale, use or other dealings
  26. in this Software without prior written authorization from the X Consortium.
  27.  
  28. */
  29.  
  30. /*
  31.  * Stephen Gildea, MIT X Consortium, January 1991
  32.  */
  33.  
  34. #include "Xlib_private.h"
  35. #include "Xcmsint.h"
  36.  
  37. #if !defined(X_NOT_STDC_ENV) && (defined(__STDC__) || !(defined(sun) || (defined(sony) && !defined(SYSTYPE_SYSV) && !defined(_SYSTYPE_SYSV))))
  38. #include <float.h>
  39. #endif
  40. #ifndef DBL_EPSILON
  41. #define DBL_EPSILON 1e-6
  42. #endif
  43.  
  44. #ifdef _X_ROOT_STATS
  45. int cbrt_loopcount;
  46. int sqrt_loopcount;
  47. #endif
  48.  
  49. /* Newton's Method:  x_n+1 = x_n - ( f(x_n) / f'(x_n) ) */
  50.  
  51.  
  52. /* for cube roots, x^3 - a = 0,  x_new = x - 1/3 (x - a/x^2) */
  53.  
  54. double
  55. _XcmsCubeRoot(a)
  56.     double a;
  57. {
  58.     register double abs_a, cur_guess, delta;
  59.  
  60. #ifdef DEBUG
  61.     printf("_XcmsCubeRoot passed in %g\n", a);
  62. #endif
  63. #ifdef _X_ROOT_STATS
  64.     cbrt_loopcount = 0;
  65. #endif
  66.     if (a == 0.)
  67.     return 0.;
  68.  
  69.     abs_a = a<0. ? -a : a;    /* convert to positive to speed loop tests */
  70.  
  71.     /* arbitrary first guess */
  72.     if (abs_a > 1.)
  73.     cur_guess = abs_a/8.;
  74.     else
  75.     cur_guess = abs_a*8.;
  76.  
  77.     do {
  78. #ifdef _X_ROOT_STATS
  79.     cbrt_loopcount++;
  80. #endif
  81.     delta = (cur_guess - abs_a/(cur_guess*cur_guess))/3.;
  82.     cur_guess -= delta;
  83.     if (delta < 0.) delta = -delta;
  84.     } while (delta >= cur_guess*DBL_EPSILON);
  85.  
  86.     if (a < 0.)
  87.     cur_guess = -cur_guess;
  88.  
  89. #ifdef DEBUG
  90.     printf("_XcmsCubeRoot returning %g\n", cur_guess);
  91. #endif
  92.     return cur_guess;
  93. }
  94.     
  95.  
  96.  
  97. /* for square roots, x^2 - a = 0,  x_new = x - 1/2 (x - a/x) */
  98.  
  99. double
  100. _XcmsSquareRoot(a)
  101.     double a;
  102. {
  103.     register double cur_guess, delta;
  104.  
  105. #ifdef DEBUG
  106.     printf("_XcmsSquareRoot passed in %g\n", a);
  107. #endif
  108. #ifdef _X_ROOT_STATS
  109.     sqrt_loopcount = 0;
  110. #endif
  111.     if (a == 0.)
  112.     return 0.;
  113.  
  114.     if (a < 0.) {
  115.     /* errno = EDOM; */
  116.     return 0.;
  117.     }
  118.  
  119.     /* arbitrary first guess */
  120.     if (a > 1.)
  121.     cur_guess = a/4.;
  122.     else
  123.     cur_guess = a*4.;
  124.  
  125.     do {
  126. #ifdef _X_ROOT_STATS
  127.     sqrt_loopcount++;
  128. #endif
  129.     delta = (cur_guess - a/cur_guess)/2.;
  130.     cur_guess -= delta;
  131.     if (delta < 0.) delta = -delta;
  132.     } while (delta >= cur_guess*DBL_EPSILON);
  133.  
  134. #ifdef DEBUG
  135.     printf("_XcmsSquareRoot returning %g\n", cur_guess);
  136. #endif
  137.     return cur_guess;
  138. }
  139.     
  140.