sqrtf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* sqrtf.c
  2. *
  3. * Square root
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, sqrtf();
  10. *
  11. * y = sqrtf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the square root of x.
  18. *
  19. * Range reduction involves isolating the power of two of the
  20. * argument and using a polynomial approximation to obtain
  21. * a rough value for the square root. Then Heron's iteration
  22. * is used three times to converge to an accurate value.
  23. *
  24. *
  25. *
  26. * ACCURACY:
  27. *
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * IEEE 0,1.e38 100000 8.7e-8 2.9e-8
  32. *
  33. *
  34. * ERROR MESSAGES:
  35. *
  36. * message condition value returned
  37. * sqrtf domain x < 0 0.0
  38. *
  39. */
  40. /*
  41. Cephes Math Library Release 2.2: June, 1992
  42. Copyright 1984, 1987, 1988, 1992 by Stephen L. Moshier
  43. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  44. */
  45. /* Single precision square root
  46. * test interval: [sqrt(2)/2, sqrt(2)]
  47. * trials: 30000
  48. * peak relative error: 8.8e-8
  49. * rms relative error: 3.3e-8
  50. *
  51. * test interval: [0.01, 100.0]
  52. * trials: 50000
  53. * peak relative error: 8.7e-8
  54. * rms relative error: 3.3e-8
  55. *
  56. * Copyright (C) 1989 by Stephen L. Moshier. All rights reserved.
  57. */
  58. #include <math.h>
  59. #ifdef ANSIC
  60. float frexpf( float, int * );
  61. float ldexpf( float, int );
  62. float sqrtf( float xx )
  63. #else
  64. float frexpf(), ldexpf();
  65. float sqrtf(xx)
  66. float xx;
  67. #endif
  68. {
  69. float f, x, y;
  70. int e;
  71. f = xx;
  72. if( f <= 0.0 )
  73. {
  74. if( f < 0.0 )
  75. mtherr( "sqrtf", DOMAIN );
  76. return( 0.0 );
  77. }
  78. x = frexpf( f, &e ); /* f = x * 2**e, 0.5 <= x < 1.0 */
  79. /* If power of 2 is odd, double x and decrement the power of 2. */
  80. if( e & 1 )
  81. {
  82. x = x + x;
  83. e -= 1;
  84. }
  85. e >>= 1; /* The power of 2 of the square root. */
  86. if( x > 1.41421356237 )
  87. {
  88. /* x is between sqrt(2) and 2. */
  89. x = x - 2.0;
  90. y =
  91. ((((( -9.8843065718E-4 * x
  92. + 7.9479950957E-4) * x
  93. - 3.5890535377E-3) * x
  94. + 1.1028809744E-2) * x
  95. - 4.4195203560E-2) * x
  96. + 3.5355338194E-1) * x
  97. + 1.41421356237E0;
  98. goto sqdon;
  99. }
  100. if( x > 0.707106781187 )
  101. {
  102. /* x is between sqrt(2)/2 and sqrt(2). */
  103. x = x - 1.0;
  104. y =
  105. ((((( 1.35199291026E-2 * x
  106. - 2.26657767832E-2) * x
  107. + 2.78720776889E-2) * x
  108. - 3.89582788321E-2) * x
  109. + 6.24811144548E-2) * x
  110. - 1.25001503933E-1) * x * x
  111. + 0.5 * x
  112. + 1.0;
  113. goto sqdon;
  114. }
  115. /* x is between 0.5 and sqrt(2)/2. */
  116. x = x - 0.5;
  117. y =
  118. ((((( -3.9495006054E-1 * x
  119. + 5.1743034569E-1) * x
  120. - 4.3214437330E-1) * x
  121. + 3.5310730460E-1) * x
  122. - 3.5354581892E-1) * x
  123. + 7.0710676017E-1) * x
  124. + 7.07106781187E-1;
  125. sqdon:
  126. y = ldexpf( y, e ); /* y = y * 2**e */
  127. return( y);
  128. }