This is a non-standard* behavior of the MinGW environment (that I have also observed, indirectly, through a customer for whom the software I work on was failing unexpectedly).
You may have seen the correct result for sqrt(-0.)
, i.e. -0.
, with a higher optimization level where the value was computed correctly at compile-time, or with a previous version of the run-time in which the bug was not present.
I ended up defining my own “fixed” sqrt()
as:
double mysqrt(double x) {
if (x == 0.) return x; else return sqrt(x);
}
(*) IEEE 754 defines sqrt(-0.)
as -0.
. A C or C++ compiler does not have to implement IEEE 754 for floating-point arithmetic, but GCC tries to (and in this particular instance, fails).