s_catanf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $OpenBSD: s_catanf.c,v 1.2 2010/07/18 18:42:26 guenther 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. /* catanf()
  18. *
  19. * Complex circular arc tangent
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * float complex catanf();
  26. * float complex z, w;
  27. *
  28. * w = catanf( z );
  29. *
  30. *
  31. *
  32. * DESCRIPTION:
  33. *
  34. * If
  35. * z = x + iy,
  36. *
  37. * then
  38. * 1 ( 2x )
  39. * Re w = - arctan(-----------) + k PI
  40. * 2 ( 2 2)
  41. * (1 - x - y )
  42. *
  43. * ( 2 2)
  44. * 1 (x + (y+1) )
  45. * Im w = - log(------------)
  46. * 4 ( 2 2)
  47. * (x + (y-1) )
  48. *
  49. * Where k is an arbitrary integer.
  50. *
  51. *
  52. *
  53. * ACCURACY:
  54. *
  55. * Relative error:
  56. * arithmetic domain # trials peak rms
  57. * IEEE -10,+10 30000 2.3e-6 5.2e-8
  58. *
  59. */
  60. #include <openlibm_complex.h>
  61. #include <openlibm_math.h>
  62. #define MAXNUMF 1.0e38F
  63. static const double DP1 = 3.140625;
  64. static const double DP2 = 9.67502593994140625E-4;
  65. static const double DP3 = 1.509957990978376432E-7;
  66. static float
  67. _redupif(float xx)
  68. {
  69. float x, t;
  70. long i;
  71. x = xx;
  72. t = x/(float)M_PI;
  73. if(t >= 0.0)
  74. t += 0.5;
  75. else
  76. t -= 0.5;
  77. i = t; /* the multiple */
  78. t = i;
  79. t = ((x - t * DP1) - t * DP2) - t * DP3;
  80. return(t);
  81. }
  82. float complex
  83. catanf(float complex z)
  84. {
  85. float complex w;
  86. float a, t, x, x2, y;
  87. x = crealf(z);
  88. y = cimagf(z);
  89. if((x == 0.0f) && (y > 1.0f))
  90. goto ovrf;
  91. x2 = x * x;
  92. a = 1.0f - x2 - (y * y);
  93. if (a == 0.0f)
  94. goto ovrf;
  95. t = 0.5f * atan2f(2.0f * x, a);
  96. w = _redupif(t);
  97. t = y - 1.0f;
  98. a = x2 + (t * t);
  99. if(a == 0.0f)
  100. goto ovrf;
  101. t = y + 1.0f;
  102. a = (x2 + (t * t))/a;
  103. w = w + (0.25f * logf (a)) * I;
  104. return (w);
  105. ovrf:
  106. /*mtherr( "catanf", OVERFLOW );*/
  107. w = MAXNUMF + MAXNUMF * I;
  108. return (w);
  109. }