Bug 2181 - csqrt() sometimes returns negative real
Summary: csqrt() sometimes returns negative real
Status: RESOLVED DUPLICATE of bug 1466
Alias: None
Product: glibc
Classification: Unclassified
Component: math (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Andreas Jaeger
URL:
Keywords:
Depends on:
Blocks: 2182
  Show dependency treegraph
 
Reported: 2006-01-19 17:37 UTC by Wes Loewer
Modified: 2016-05-20 19:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wes Loewer 2006-01-19 17:37:20 UTC
ANSI-C99 7.3.8.3.3 says that the real part of the result csqrt() cannot be
negative. However, when the argument's real part is zero and the imag part is
negative, such as csqrt(0.0-1.0*I), it returns a negative real.  

Looking at the s_csqrt.c (and s_csqrtf.c and s_csqrtl.c)

---------- s_csqrt.c near line 78 ----------

      else if (rcls == FP_ZERO)
    {
      double r = __ieee754_sqrt (0.5 * fabs (__imag__ x));

>>>      __real__ res = __copysign (r, __imag__ x);
>>>      __imag__ res = r;
    }

---------------------------------------------

The two lines marked with >>> appear to be the problem.  When the real part of
the argument is zero, then the real part of the result should be positive and
the imag part of the result should have the same sign as the imag part of the
argument, and both parts of the result should have the same magnitude.  In other
words...

---------- suggested s_csqrt.c near line 78 ----------

      else if (rcls == FP_ZERO)
    {
      double r = __ieee754_sqrt (0.5 * fabs (__imag__ x));

>>>      __real__ res = r;
>>>      __imag__ res = __copysign (r, __imag__ x);
    }

---------------------------------------------

Please test this of course, but I believe this will solve the problem.

Once this is fixed, all the other functions that depend on csqrt() should be
tested. For example, the recently patched cacosh()(bug#2153) will need to be
fixed again.

-Wes Loewer
Comment 1 Ulrich Drepper 2006-04-23 17:20:58 UTC

*** This bug has been marked as a duplicate of 1466 ***