s_casinf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* $OpenBSD: s_casinf.c,v 1.3 2011/07/20 19:28:33 martynas 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. /* casinf()
  18. *
  19. * Complex circular arc sine
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * void casinf();
  26. * cmplxf z, w;
  27. *
  28. * casinf( &z, &w );
  29. *
  30. *
  31. *
  32. * DESCRIPTION:
  33. *
  34. * Inverse complex sine:
  35. *
  36. * 2
  37. * w = -i clog( iz + csqrt( 1 - z ) ).
  38. *
  39. *
  40. * ACCURACY:
  41. *
  42. * Relative error:
  43. * arithmetic domain # trials peak rms
  44. * IEEE -10,+10 30000 1.1e-5 1.5e-6
  45. * Larger relative error can be observed for z near zero.
  46. *
  47. */
  48. #include <openlibm.h>
  49. #include <openlibm_complex.h>
  50. float complex
  51. casinf(float complex z)
  52. {
  53. float complex w;
  54. float x, y;
  55. static float complex ca, ct, zz, z2;
  56. /*
  57. float cn, n;
  58. static float a, b, s, t, u, v, y2;
  59. static cmplxf sum;
  60. */
  61. x = crealf(z);
  62. y = cimagf(z);
  63. if(y == 0.0f) {
  64. if(fabsf(x) > 1.0f) {
  65. w = (float)M_PI_2 + 0.0f * I;
  66. /*mtherr( "casinf", DOMAIN );*/
  67. }
  68. else {
  69. w = asinf (x) + 0.0f * I;
  70. }
  71. return (w);
  72. }
  73. /* Power series expansion */
  74. /*
  75. b = cabsf(z);
  76. if(b < 0.125) {
  77. z2.r = (x - y) * (x + y);
  78. z2.i = 2.0 * x * y;
  79. cn = 1.0;
  80. n = 1.0;
  81. ca.r = x;
  82. ca.i = y;
  83. sum.r = x;
  84. sum.i = y;
  85. do {
  86. ct.r = z2.r * ca.r - z2.i * ca.i;
  87. ct.i = z2.r * ca.i + z2.i * ca.r;
  88. ca.r = ct.r;
  89. ca.i = ct.i;
  90. cn *= n;
  91. n += 1.0;
  92. cn /= n;
  93. n += 1.0;
  94. b = cn/n;
  95. ct.r *= b;
  96. ct.i *= b;
  97. sum.r += ct.r;
  98. sum.i += ct.i;
  99. b = fabsf(ct.r) + fabsf(ct.i);
  100. }
  101. while(b > MACHEPF);
  102. w->r = sum.r;
  103. w->i = sum.i;
  104. return;
  105. }
  106. */
  107. ca = x + y * I;
  108. ct = ca * I; /* iz */
  109. /* sqrt( 1 - z*z) */
  110. /* cmul( &ca, &ca, &zz ) */
  111. /*x * x - y * y */
  112. zz = (x - y) * (x + y) + (2.0f * x * y) * I;
  113. zz = 1.0f - crealf(zz) - cimagf(zz) * I;
  114. z2 = csqrtf (zz);
  115. zz = ct + z2;
  116. zz = clogf (zz);
  117. /* multiply by 1/i = -i */
  118. w = zz * (-1.0f * I);
  119. return (w);
  120. }