/newlib/libm/complex/catan.c

https://bitbucket.org/pizzafactory/binutils · C · 130 lines · 36 code · 13 blank · 81 comment · 7 complexity · b0d6204bf3926d5ce9878bc88829f855 MD5 · raw file

  1. /* $NetBSD: catan.c,v 1.1 2007/08/20 16:01:32 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. * imported and modified include for newlib 2010/10/03
  31. * Marco Atzeri <marco_atzeri@yahoo.it>
  32. */
  33. /*
  34. FUNCTION
  35. <<catan>>, <<catanf>>---complex arc tangent
  36. INDEX
  37. catan
  38. INDEX
  39. catanf
  40. ANSI_SYNOPSIS
  41. #include <complex.h>
  42. double complex catan(double complex <[z]>);
  43. float complex catanf(float complex <[z]>);
  44. DESCRIPTION
  45. @ifnottex
  46. These functions compute the complex arc tangent of <[z]>,
  47. with branch cuts outside the interval [-i, +i] along the
  48. imaginary axis.
  49. @end ifnottex
  50. @tex
  51. These functions compute the complex arc tangent of <[z]>,
  52. with branch cuts outside the interval [$-i$, $+i$] along the
  53. imaginary axis.
  54. @end tex
  55. <<catanf>> is identical to <<catan>>, except that it performs
  56. its calculations on <<floats complex>>.
  57. RETURNS
  58. @ifnottex
  59. These functions return the complex arc tangent value, in the range
  60. of a strip mathematically unbounded along the imaginary axis
  61. and in the interval [-pi/2, +pi/2] along the real axis.
  62. @end ifnottex
  63. @tex
  64. These functions return the complex arc tangent, in the range
  65. of a strip mathematically unbounded along the imaginary axis
  66. and in the interval [$-\pi/2$, $+\pi/2$] along the real axis.
  67. @end tex
  68. PORTABILITY
  69. <<catan>> and <<catanf>> are ISO C99
  70. QUICKREF
  71. <<catan>> and <<catanf>> are ISO C99
  72. */
  73. #include <complex.h>
  74. #include <math.h>
  75. #include "cephes_subr.h"
  76. #ifdef __weak_alias
  77. __weak_alias(catan, _catan)
  78. #endif
  79. double complex
  80. catan(double complex z)
  81. {
  82. double complex w;
  83. double a, t, x, x2, y;
  84. x = creal(z);
  85. y = cimag(z);
  86. if ((x == 0.0) && (y > 1.0))
  87. goto ovrf;
  88. x2 = x * x;
  89. a = 1.0 - x2 - (y * y);
  90. if (a == 0.0)
  91. goto ovrf;
  92. t = 0.5 * atan2(2.0 * x, a);
  93. w = _redupi(t);
  94. t = y - 1.0;
  95. a = x2 + (t * t);
  96. if (a == 0.0)
  97. goto ovrf;
  98. t = y + 1.0;
  99. a = (x2 + (t * t))/a;
  100. w = w + (0.25 * log(a)) * I;
  101. return w;
  102. ovrf:
  103. #if 0
  104. mtherr ("catan", OVERFLOW);
  105. #endif
  106. w = HUGE_VAL + HUGE_VAL * I;
  107. return w;
  108. }