Skip to content

Commit 1ecf5b0

Browse files
[do-not-upstream] complex: Fix catan() functions with pure imaginary inputs
`catan()` functions needed special case handling for pure imaginary inputs instead of triggering the overflow case when it shouldn't Signed-off-by: Mostafa Salman <[email protected]>
1 parent 0c6c54d commit 1ecf5b0

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

newlib/libm/complex/catan.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434

3535
/*
3636
FUNCTION
37-
<<catan>>, <<catanf>>---complex arc tangent
37+
<<catan>>, <<catanf>>---complex arc tangent
3838
3939
INDEX
40-
catan
40+
catan
4141
INDEX
42-
catanf
42+
catanf
4343
4444
SYNOPSIS
4545
#include <complex.h>
@@ -48,37 +48,37 @@ SYNOPSIS
4848
4949
5050
DESCRIPTION
51-
@ifnottex
52-
These functions compute the complex arc tangent of <[z]>,
53-
with branch cuts outside the interval [-i, +i] along the
54-
imaginary axis.
55-
@end ifnottex
56-
@tex
57-
These functions compute the complex arc tangent of <[z]>,
58-
with branch cuts outside the interval [$-i$, $+i$] along the
59-
imaginary axis.
60-
@end tex
61-
62-
<<catanf>> is identical to <<catan>>, except that it performs
63-
its calculations on <<floats complex>>.
51+
@ifnottex
52+
These functions compute the complex arc tangent of <[z]>,
53+
with branch cuts outside the interval [-i, +i] along the
54+
imaginary axis.
55+
@end ifnottex
56+
@tex
57+
These functions compute the complex arc tangent of <[z]>,
58+
with branch cuts outside the interval [$-i$, $+i$] along the
59+
imaginary axis.
60+
@end tex
61+
62+
<<catanf>> is identical to <<catan>>, except that it performs
63+
its calculations on <<floats complex>>.
6464
6565
RETURNS
66-
@ifnottex
67-
These functions return the complex arc tangent value, in the range
68-
of a strip mathematically unbounded along the imaginary axis
69-
and in the interval [-pi/2, +pi/2] along the real axis.
70-
@end ifnottex
71-
@tex
72-
These functions return the complex arc tangent, in the range
73-
of a strip mathematically unbounded along the imaginary axis
74-
and in the interval [$-\pi/2$, $+\pi/2$] along the real axis.
75-
@end tex
66+
@ifnottex
67+
These functions return the complex arc tangent value, in the range
68+
of a strip mathematically unbounded along the imaginary axis
69+
and in the interval [-pi/2, +pi/2] along the real axis.
70+
@end ifnottex
71+
@tex
72+
These functions return the complex arc tangent, in the range
73+
of a strip mathematically unbounded along the imaginary axis
74+
and in the interval [$-\pi/2$, $+\pi/2$] along the real axis.
75+
@end tex
7676
7777
PORTABILITY
78-
<<catan>> and <<catanf>> are ISO C99
78+
<<catan>> and <<catanf>> are ISO C99
7979
8080
QUICKREF
81-
<<catan>> and <<catanf>> are ISO C99
81+
<<catan>> and <<catanf>> are ISO C99
8282
8383
*/
8484

@@ -100,8 +100,17 @@ catan(double complex z)
100100
x = creal(z);
101101
y = cimag(z);
102102

103-
if ((x == 0.0) && (y > 1.0))
104-
goto ovrf;
103+
if (x == 0.0) {
104+
if (y > 1.0) {
105+
return CMPLX(M_PI_2, 0.5 * log((1.0 + y)/(y - 1.0)));
106+
}
107+
if (y < -1.0) {
108+
return CMPLX(-M_PI_2, 0.5 * log((1.0 - y)/(-y - 1.0)));
109+
}
110+
if (fabs(y) <= 1.0) {
111+
return CMPLX(0.0, atanh(y));
112+
}
113+
}
105114

106115
x2 = x * x;
107116
a = 1.0 - x2 - (y * y);

newlib/libm/complex/catanf.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ catanf(float complex z)
4949
x = crealf(z);
5050
y = cimagf(z);
5151

52-
if ((x == 0.0f) && (y > 1.0f))
53-
goto ovrf;
52+
if (x == 0.0f) {
53+
if (y > 1.0f) {
54+
return CMPLXF((float)M_PI_2, 0.5f * logf((1.0f + y)/(y - 1.0f)));
55+
}
56+
if (y < -1.0) {
57+
return CMPLXF((float)-M_PI_2, 0.5f * logf((1.0f - y)/(-y - 1.0f)));
58+
}
59+
if (fabsf(y) <= 1.0f) {
60+
return CMPLXF(0.0f, atanhf(y));
61+
}
62+
}
5463

5564
x2 = x * x;
5665
a = 1.0f - x2 - (y * y);

newlib/libm/complex/catanl.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ catanl(long double complex z)
4848
x = creall(z);
4949
y = cimagl(z);
5050

51-
if ((x == 0.0L) && (y > 1.0L))
52-
goto ovrf;
51+
if (x == 0.0L) {
52+
if (y > 1.0L) {
53+
return CMPLXL(_M_PI_2L, 0.5L * logl((1.0L + y)/(y - 1.0L)));
54+
}
55+
if (y < -1.0L) {
56+
return CMPLXL(-_M_PI_2L, 0.5L * logl((1.0L - y)/(-y - 1.0L)));
57+
}
58+
if (fabsl(y) <= 1.0L) {
59+
return CMPLXL(0.0L, atanhl(y));
60+
}
61+
}
5362

5463
x2 = x * x;
5564
a = 1.0L - x2 - (y * y);

0 commit comments

Comments
 (0)