On 2017/3/24 23:58, Liu Hao wrote:
> On 2017/3/24 23:08, Jeroen Ooms wrote:
>> I was able to reproduce the problem on msys2 with gcc 6.3:
>>
>> #include <iostream>
>> #include <cmath>
>> #include <complex>
>>
>> int main(){
>> std::cout << std::fixed;
>> std::complex<double> z(356, 0);
>> std::cout << "tanh" << z << " = " << std::tanh(z)
>> << " (tanh(356) = " << std::tanh(356) << ")\n";
>> }
>>
>> On Linux / OS-X we get:
>>
>> tanh(356.000000,0.000000) = (1.000000,-0.000000) (tanh(356) = 1.000000)
>>
>> But on Windows we get:
>>
>> tanh(356.000000,0.000000) = (nan,0.000000) (tanh(356) = 1.000000)
> After a little investigation the problem is located here:
>
> In mingw-w64-crt\complex\ctanh.def.h:
>
> __FLT_ABI(sincos) (__FLT_CST(2.0) * __imag__ z, &s, &c);
>
> d = (__FLT_ABI(cosh) (__FLT_CST(2.0) * __real__ z) + c);
>
> if (d == __FLT_CST(0.0))
> {
> __complex__ __FLT_TYPE ez = __FLT_ABI(cexp) (z);
> __complex__ __FLT_TYPE emz = __FLT_ABI(cexp) (-z);
>
> return (ez - emz) / (ez + emz);
> }
>
> __real__ ret = __FLT_ABI(sinh) (__FLT_CST(2.0) * __real__ z) / d;
>
> Given `__real__ z` is 356, `cosh(712)` is called, which yields positive
> infinity. Since it doesn't compare equal with zero, the `if` branch is
> not taken and the result of `sinh(712)`, which is also positive
> infinity, is divided by the previous result, yielding a NaN.
>
> In the code above `ret` is normally set to `sinh(2 * __real__ z) /
> cosh(2 * __real__ z)`. Acknowledging the fact that both can be NaNs we
> can replace it with `tanh(2 * __real__ z)` directly ({sin,cos,tan}h
> functions are part of MSVCRT). The attached patch should fix the problem.
>
> I am not a mathematician, though. So please correct me.
Doug, would you please have a look?
--
Best regards,
LH_Mouse
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public