s_catan.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* $OpenBSD: s_catan.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. /* catan()
  18. *
  19. * Complex circular arc tangent
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * double complex catan();
  26. * double complex z, w;
  27. *
  28. * w = catan (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. * catan(z) = -i catanh(iz).
  52. *
  53. * ACCURACY:
  54. *
  55. * Relative error:
  56. * arithmetic domain # trials peak rms
  57. * DEC -10,+10 5900 1.3e-16 7.8e-18
  58. * IEEE -10,+10 30000 2.3e-15 8.5e-17
  59. * The check catan( ctan(z) ) = z, with |x| and |y| < PI/2,
  60. * had peak relative error 1.5e-16, rms relative error
  61. * 2.9e-17. See also clog().
  62. */
  63. #include <float.h>
  64. #include <openlibm_complex.h>
  65. #include <openlibm_math.h>
  66. #define MAXNUM 1.0e308
  67. static const double DP1 = 3.14159265160560607910E0;
  68. static const double DP2 = 1.98418714791870343106E-9;
  69. static const double DP3 = 1.14423774522196636802E-17;
  70. static double
  71. _redupi(double x)
  72. {
  73. double t;
  74. long i;
  75. t = x/M_PI;
  76. if(t >= 0.0)
  77. t += 0.5;
  78. else
  79. t -= 0.5;
  80. i = t; /* the multiple */
  81. t = i;
  82. t = ((x - t * DP1) - t * DP2) - t * DP3;
  83. return (t);
  84. }
  85. double complex
  86. catan(double complex z)
  87. {
  88. double complex w;
  89. double a, t, x, x2, y;
  90. x = creal (z);
  91. y = cimag (z);
  92. if ((x == 0.0) && (y > 1.0))
  93. goto ovrf;
  94. x2 = x * x;
  95. a = 1.0 - x2 - (y * y);
  96. if (a == 0.0)
  97. goto ovrf;
  98. t = 0.5 * atan2 (2.0 * x, a);
  99. w = _redupi (t);
  100. t = y - 1.0;
  101. a = x2 + (t * t);
  102. if (a == 0.0)
  103. goto ovrf;
  104. t = y + 1.0;
  105. a = (x2 + (t * t))/a;
  106. w = w + (0.25 * log (a)) * I;
  107. return (w);
  108. ovrf:
  109. /*mtherr ("catan", OVERFLOW);*/
  110. w = MAXNUM + MAXNUM * I;
  111. return (w);
  112. }
  113. #if LDBL_MANT_DIG == DBL_MANT_DIG
  114. __strong_alias(catanl, catan);
  115. #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */