/libm/complex/casinf.c

https://gitlab.com/abidin24/sortix · C · 120 lines · 37 code · 11 blank · 72 comment · 4 complexity · 85df341fe983f31f4e5083217346b717 MD5 · raw file

  1. /* $NetBSD: casinf.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 "../src/namespace.h"
  31. #include <complex.h>
  32. #include <math.h>
  33. #ifdef __weak_alias
  34. __weak_alias(casinf, _casinf)
  35. #endif
  36. float complex
  37. casinf(float complex z)
  38. {
  39. float complex w;
  40. float complex ca, ct, zz, z2;
  41. float x, y;
  42. x = crealf(z);
  43. y = cimagf(z);
  44. #if 0 /* MD: test is incorrect, casin(>1) is defined */
  45. if (y == 0.0f) {
  46. if (fabsf(x) > 1.0) {
  47. w = M_PI_2 + 0.0f * I;
  48. #if 0
  49. mtherr ("casin", DOMAIN);
  50. #endif
  51. } else {
  52. w = asinf(x) + 0.0f * I;
  53. }
  54. return w;
  55. }
  56. #endif
  57. /* Power series expansion */
  58. /*
  59. b = cabsf(z);
  60. if( b < 0.125 )
  61. {
  62. z2.r = (x - y) * (x + y);
  63. z2.i = 2.0 * x * y;
  64. cn = 1.0;
  65. n = 1.0;
  66. ca.r = x;
  67. ca.i = y;
  68. sum.r = x;
  69. sum.i = y;
  70. do
  71. {
  72. ct.r = z2.r * ca.r - z2.i * ca.i;
  73. ct.i = z2.r * ca.i + z2.i * ca.r;
  74. ca.r = ct.r;
  75. ca.i = ct.i;
  76. cn *= n;
  77. n += 1.0;
  78. cn /= n;
  79. n += 1.0;
  80. b = cn/n;
  81. ct.r *= b;
  82. ct.i *= b;
  83. sum.r += ct.r;
  84. sum.i += ct.i;
  85. b = fabsf(ct.r) + fabsf(ct.i);
  86. }
  87. while( b > MACHEP );
  88. w->r = sum.r;
  89. w->i = sum.i;
  90. return;
  91. }
  92. */
  93. ca = x + y * I;
  94. ct = ca * I;
  95. /* sqrt( 1 - z*z) */
  96. /* cmul( &ca, &ca, &zz ) */
  97. /*x * x - y * y */
  98. zz = (x - y) * (x + y) + (2.0f * x * y) * I;
  99. zz = 1.0f - crealf(zz) - cimagf(zz) * I;
  100. z2 = csqrtf(zz);
  101. zz = ct + z2;
  102. zz = clogf(zz);
  103. /* multiply by 1/i = -i */
  104. w = zz * (-1.0f * I);
  105. return w;
  106. }