Why does not std::sqrt() work correctly on double data in C++?

Here is a simple code in C++ which I am compiling using TDM-GCC 5.1 32bit (gcc-5.1.0-tdm-1-core) on a Windows 8.1 64bit Intel core i-7 machine.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double a, b;
    a = 0.002;
    b = 0.004;
    cout << std::sqrt(a*a+b*b)<< endl;
    return 0;
}

But on running, it gives the following output

nan

However, it works correctly with TDM-GCC 32bit compiler on a 32 bit machine.

Also, when I am changing the datatype to float, it is working properly on both 32bit and 64 bit computers.

What is wrong here?

Answers


The problem is as follows:

It was compiled inherently in the Code::Blocks with the following command (narrowed):

g++.exe -L"C:\Program Files (x86)\GnuWin32\lib" -o main.exe main.cpp -lm

where C:\Program Files (x86)\GnuWin32\lib has an old math library named libm.a.

So, on putting -lm along with -L"C:\Program Files (x86)\GnuWin32\lib", it is found using Process Explorer that main.exe is using that corresponding old libm5.dll which seems to be incompatible.

So, if at least one of the options -lm or -L"C:\Program Files (x86)\GnuWin32\lib" is removed and compiled as follows:

g++.exe -L"C:\Program Files (x86)\GnuWin32\lib" -o main.exe main.cpp

or

g++.exe -o main.exe main.cpp -lm

or

g++.exe -o main.exe main.cpp 

it is not linking the old libm.a and hence running correctly.


Need Your Help

Upgrade to msysgit 1.7.0.2?

upgrade msysgit

I know this question is probably stoopid. But I just don't want to cause any hickups with my work system (Win7).

Open Modal window in Center of Parent Form in MDI Application

c# winforms mdi

I am working on Winform application and want to open modal form in center of parent form. In Winform application there is :