/lib/libm/complex/casin.c

https://bitbucket.org/cooljeanius/dragonflybsd · C · 120 lines · 36 code · 12 blank · 72 comment · 4 complexity · 8081485ae2834823bd75a847bdc9ab32 MD5 · raw file

  1. /* $NetBSD: casin.c,v 1.1 2007/08/20 16:01:31 drochner Exp $ */
  2. /*-
  3. * Copyright (c) 2007 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software written by Stephen L. Moshier.
  7. * It is redistributed by the NetBSD Foundation by permission of the author.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <complex.h>
  31. #include <math.h>
  32. #ifdef __weak_alias
  33. __weak_alias(casin, _casin)
  34. #endif
  35. double complex
  36. casin(double complex z)
  37. {
  38. double complex w;
  39. double complex ca, ct, zz, z2;
  40. double x, y;
  41. x = creal(z);
  42. y = cimag(z);
  43. #if 0 /* MD: test is incorrect, casin(>1) is defined */
  44. if (y == 0.0) {
  45. if (fabs(x) > 1.0) {
  46. w = M_PI_2 + 0.0 * I;
  47. #if 0
  48. mtherr ("casin", DOMAIN);
  49. #endif
  50. } else {
  51. w = asin(x) + 0.0 * I;
  52. }
  53. return w;
  54. }
  55. #endif
  56. /* Power series expansion */
  57. /*
  58. b = cabs(z);
  59. if( b < 0.125 )
  60. {
  61. z2.r = (x - y) * (x + y);
  62. z2.i = 2.0 * x * y;
  63. cn = 1.0;
  64. n = 1.0;
  65. ca.r = x;
  66. ca.i = y;
  67. sum.r = x;
  68. sum.i = y;
  69. do
  70. {
  71. ct.r = z2.r * ca.r - z2.i * ca.i;
  72. ct.i = z2.r * ca.i + z2.i * ca.r;
  73. ca.r = ct.r;
  74. ca.i = ct.i;
  75. cn *= n;
  76. n += 1.0;
  77. cn /= n;
  78. n += 1.0;
  79. b = cn/n;
  80. ct.r *= b;
  81. ct.i *= b;
  82. sum.r += ct.r;
  83. sum.i += ct.i;
  84. b = fabs(ct.r) + fabs(ct.i);
  85. }
  86. while( b > MACHEP );
  87. w->r = sum.r;
  88. w->i = sum.i;
  89. return;
  90. }
  91. */
  92. ca = x + y * I;
  93. ct = ca * I;
  94. /* sqrt( 1 - z*z) */
  95. /* cmul( &ca, &ca, &zz ) */
  96. /*x * x - y * y */
  97. zz = (x - y) * (x + y) + (2.0 * x * y) * I;
  98. zz = 1.0 - creal(zz) - cimag(zz) * I;
  99. z2 = csqrt(zz);
  100. zz = ct + z2;
  101. zz = clog(zz);
  102. /* multiply by 1/i = -i */
  103. w = zz * (-1.0 * I);
  104. return w;
  105. }