Strange cbrt () result on linux in C

Why are these two return values ​​of the cbrt () function 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.0000000000000000

+4
source share
1 answer

Congratulations, this is a compiler error. The compiler optimizes your code by evaluating one of the cbrtcalls ahead of time, unfortunately, the version of the compiler cbrtis different from your version in libm. You will also notice that the transfer -O2results in the result v2being “wrong” (although this is mathematically correct).

,

cc (Debian 6.3.0-5) 6.3.0 20170124

(https://gcc.gnu.org/bugs/), .

+3

Source: https://habr.com/ru/post/1669201/


All Articles