sqrt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package cmplx
  5. import "math"
  6. // The original C code, the long comment, and the constants
  7. // below are from http://netlib.sandia.gov/cephes/c9x-complex/clog.c.
  8. // The go code is a simplified version of the original C.
  9. //
  10. // Cephes Math Library Release 2.8: June, 2000
  11. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  12. //
  13. // The readme file at http://netlib.sandia.gov/cephes/ says:
  14. // Some software in this archive may be from the book _Methods and
  15. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  16. // International, 1989) or from the Cephes Mathematical Library, a
  17. // commercial product. In either event, it is copyrighted by the author.
  18. // What you see here may be used freely but it comes with no support or
  19. // guarantee.
  20. //
  21. // The two known misprints in the book are repaired here in the
  22. // source listings for the gamma function and the incomplete beta
  23. // integral.
  24. //
  25. // Stephen L. Moshier
  26. // moshier@na-net.ornl.gov
  27. // Complex square root
  28. //
  29. // DESCRIPTION:
  30. //
  31. // If z = x + iy, r = |z|, then
  32. //
  33. // 1/2
  34. // Re w = [ (r + x)/2 ] ,
  35. //
  36. // 1/2
  37. // Im w = [ (r - x)/2 ] .
  38. //
  39. // Cancellation error in r-x or r+x is avoided by using the
  40. // identity 2 Re w Im w = y.
  41. //
  42. // Note that -w is also a square root of z. The root chosen
  43. // is always in the right half plane and Im w has the same sign as y.
  44. //
  45. // ACCURACY:
  46. //
  47. // Relative error:
  48. // arithmetic domain # trials peak rms
  49. // DEC -10,+10 25000 3.2e-17 9.6e-18
  50. // IEEE -10,+10 1,000,000 2.9e-16 6.1e-17
  51. // Sqrt returns the square root of x.
  52. // The result r is chosen so that real(r) ≥ 0 and imag(r) has the same sign as imag(x).
  53. func Sqrt(x complex128) complex128 {
  54. if imag(x) == 0 {
  55. // Ensure that imag(r) has the same sign as imag(x) for imag(x) == signed zero.
  56. if real(x) == 0 {
  57. return complex(0, imag(x))
  58. }
  59. if real(x) < 0 {
  60. return complex(0, math.Copysign(math.Sqrt(-real(x)), imag(x)))
  61. }
  62. return complex(math.Sqrt(real(x)), imag(x))
  63. } else if math.IsInf(imag(x), 0) {
  64. return complex(math.Inf(1.0), imag(x))
  65. }
  66. if real(x) == 0 {
  67. if imag(x) < 0 {
  68. r := math.Sqrt(-0.5 * imag(x))
  69. return complex(r, -r)
  70. }
  71. r := math.Sqrt(0.5 * imag(x))
  72. return complex(r, r)
  73. }
  74. a := real(x)
  75. b := imag(x)
  76. var scale float64
  77. // Rescale to avoid internal overflow or underflow.
  78. if math.Abs(a) > 4 || math.Abs(b) > 4 {
  79. a *= 0.25
  80. b *= 0.25
  81. scale = 2
  82. } else {
  83. a *= 1.8014398509481984e16 // 2**54
  84. b *= 1.8014398509481984e16
  85. scale = 7.450580596923828125e-9 // 2**-27
  86. }
  87. r := math.Hypot(a, b)
  88. var t float64
  89. if a > 0 {
  90. t = math.Sqrt(0.5*r + 0.5*a)
  91. r = scale * math.Abs((0.5*b)/t)
  92. t *= scale
  93. } else {
  94. r = math.Sqrt(0.5*r - 0.5*a)
  95. t = scale * math.Abs((0.5*b)/r)
  96. r *= scale
  97. }
  98. if b < 0 {
  99. return complex(t, -r)
  100. }
  101. return complex(t, r)
  102. }