Why these two return value of the cbrt() function are different ?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
double nb = 56623104;
double v1 = cbrt(nb);
printf("v1 -> %.15f\n",v1);
double v2 = cbrt((double) 56623104);
printf("v2 -> %.15f\n",v2);
}
Compilation :
gcc toto.c -o toto -lm && ./toto
Result :
v1 -> 384.000000000000057
v2 -> 384.000000000000000
Congratulations, this is a compiler bug. The compiler is optimizing your code by evaluating one of the cbrt
calls ahead of time, unfortunately, the compiler's version of cbrt
is different from your version in libm. You will also notice that passing -O2
causes the v2
result to be "wrong" as well (even though it's right, mathematically).
I verified that the bug exists on my system
cc (Debian 6.3.0-5) 6.3.0 20170124
This bug should be reported to the compiler developers (https://gcc.gnu.org/bugs/), but it's a good idea to search the bug repository first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With