diff --git a/liboctave/numeric/lo-mappers.cc b/liboctave/numeric/lo-mappers.cc --- a/liboctave/numeric/lo-mappers.cc +++ b/liboctave/numeric/lo-mappers.cc @@ -184,7 +184,20 @@ { static Complex i (0, 1); - return -i * (log (x + i * (sqrt (1.0 - x*x)))); + Complex tmp; + + if (imag (x) == 0.0) + { + // If the imaginary part of X is 0, then avoid generating an + // imaginary part of -0 for the expression 1-x*x. + + double t1 = real (x); + tmp = Complex (1.0 - t1*t1); + } + else + tmp = 1.0 - x*x; + + return -i * (log (x + i * sqrt (tmp))); } Complex @@ -198,7 +211,20 @@ { static Complex i (0, 1); - return -i * log (i*x + sqrt (1.0 - x*x)); + Complex tmp; + + if (imag (x) == 0.0) + { + // If the imaginary part of X is 0, then avoid generating an + // imaginary part of -0 for the expression 1-x*x. + + double t1 = real (x); + tmp = Complex (1.0 - t1*t1); + } + else + tmp = 1.0 - x*x; + + return -i * log (i*x + sqrt (tmp)); } Complex @@ -401,7 +427,20 @@ { static FloatComplex i (0, 1); - return -i * (log (x + i * (sqrt (static_cast(1.0) - x*x)))); + FloatComplex tmp; + + if (imag (x) == 0.0) + { + // If the imaginary part of X is 0, then avoid generating an + // imaginary part of -0 for the expression 1-x*x. + + double t1 = real (x); + tmp = FloatComplex (1.0f - t1*t1); + } + else + tmp = 1.0f - x*x; + + return -i * (log (x + i * (sqrt (tmp)))); } FloatComplex @@ -415,7 +454,20 @@ { static FloatComplex i (0, 1); - return -i * log (i*x + sqrt (static_cast(1.0) - x*x)); + FloatComplex tmp; + + if (imag (x) == 0.0) + { + // If the imaginary part of X is 0, then avoid generating an + // imaginary part of -0 for the expression 1-x*x. + + double t1 = real (x); + tmp = FloatComplex (1.0f - t1*t1); + } + else + tmp = 1.0f - x*x; + + return -i * log (i*x + sqrt (tmp)); } FloatComplex