log.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 natural logarithm
  28. //
  29. // DESCRIPTION:
  30. //
  31. // Returns complex logarithm to the base e (2.718...) of
  32. // the complex argument z.
  33. //
  34. // If
  35. // z = x + iy, r = sqrt( x**2 + y**2 ),
  36. // then
  37. // w = log(r) + i arctan(y/x).
  38. //
  39. // The arctangent ranges from -PI to +PI.
  40. //
  41. // ACCURACY:
  42. //
  43. // Relative error:
  44. // arithmetic domain # trials peak rms
  45. // DEC -10,+10 7000 8.5e-17 1.9e-17
  46. // IEEE -10,+10 30000 5.0e-15 1.1e-16
  47. //
  48. // Larger relative error can be observed for z near 1 +i0.
  49. // In IEEE arithmetic the peak absolute error is 5.2e-16, rms
  50. // absolute error 1.0e-16.
  51. // Log returns the natural logarithm of x.
  52. func Log(x complex128) complex128 {
  53. return complex(math.Log(Abs(x)), Phase(x))
  54. }
  55. // Log10 returns the decimal logarithm of x.
  56. func Log10(x complex128) complex128 {
  57. z := Log(x)
  58. return complex(math.Log10E*real(z), math.Log10E*imag(z))
  59. }