Question

I'm developing a unit test for my application, but I'm faced with a strange problem I don't understand.

The code is:

double res = BytesTool::convertSize(1, BytesTool::Byte, BytesTool::KiloByte);
double tmp = pow((double)1000, -1);
QVERIFY(res == tmp);

I'm compiling from a Linux machine (host 64bits) for Linux 64bits with gcc (host 64bits) and cross compiling for Windows 32bits with Linux's mingw32 compiler.

The program is working fine (assertion success) with the Linux compilation in debug and release mode. For the Windows version, it is working fine in the debug version, but not for the release version; the assertion fails.

The strange part is, if I insert a trace, the test works in Windows:

double res = BytesTool::convertSize(1, BytesTool::Byte, BytesTool::KiloByte);
printf("test");
double tmp = pow((double)1000, -1);
QVERIFY(res == tmp); // Is TRUE when printf("test") is present, FALSE otherwise

I'm lost and I really don't understand what is happening. Why does printf make it work?

Thank for your help.

Was it helpful?

Solution

printf will cause it to work because the floating point number will be converted from the internal FPU 80-bit representation (assuming x86 "old-style" math) to the 64-bit that is saved in a double.

The reason for this is that the register value has to be moved to the stack when you call another function (again, assuming x86 old-style FPU calling conventions), which will cause it to be rounded to 64bits of precision.

Your other compilations most likely works because they are using SSE2+ math, which has a native 64-bit floating point type.

== tests that the floats are identical, which is almost never the correct thing to do with floating point numbers.

In this case it is not since the internal CPU representation differs from the one that is stored in a double.

When comparing floating point numbers, always check if they are close enough to each other instead of being equal.

#include <math.h>

QVERIFY( fabs(res-tmp) < DBL_EPSILON )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top