s_cpow.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* $OpenBSD: s_cpow.c,v 1.6 2013/07/03 04:46:36 espie Exp $ */
  2. /*
  3. * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* cpow
  18. *
  19. * Complex power function
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * double complex cpow();
  26. * double complex a, z, w;
  27. *
  28. * w = cpow (a, z);
  29. *
  30. *
  31. *
  32. * DESCRIPTION:
  33. *
  34. * Raises complex A to the complex Zth power.
  35. * Definition is per AMS55 # 4.2.8,
  36. * analytically equivalent to cpow(a,z) = cexp(z clog(a)).
  37. *
  38. * ACCURACY:
  39. *
  40. * Relative error:
  41. * arithmetic domain # trials peak rms
  42. * IEEE -10,+10 30000 9.4e-15 1.5e-15
  43. *
  44. */
  45. #include <complex.h>
  46. #include <float.h>
  47. #include <openlibm.h>
  48. #include "math_private.h"
  49. DLLEXPORT double complex
  50. cpow(double complex a, double complex z)
  51. {
  52. double complex w;
  53. double x, y, r, theta, absa, arga;
  54. x = creal (z);
  55. y = cimag (z);
  56. absa = cabs (a);
  57. if (absa == 0.0) {
  58. return (0.0 + 0.0 * I);
  59. }
  60. arga = carg (a);
  61. r = pow (absa, x);
  62. theta = x * arga;
  63. if (y != 0.0) {
  64. r = r * exp (-y * arga);
  65. theta = theta + y * log (absa);
  66. }
  67. w = r * cos (theta) + (r * sin (theta)) * I;
  68. return (w);
  69. }
  70. #if LDBL_MANT_DIG == DBL_MANT_DIG
  71. __strong_alias(cpowl, cpow);
  72. #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */