[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] It's impossible !!!!! I don't know !!!! This is correct !!!



Vincenzo Mazzotta wrote:
Hi to all ...I've a problem if i sum two or much variable with class operation :       double a = ((double) (now)) + ((double) (delay_line[line]));
      double b = ((double) (punt->pkt.arriveTime)) + ((double) (delay_line[punt->pkt.linedelay])) + ((double) (txtime(punt->pkt.pkt)));
      fprintf(stderr,"%g,%g,%g,%g,%g %g %g\n",now,delay_line[line],punt->pkt.arriveTime,delay_line[punt->pkt.linedelay],txtime(punt->pkt.pkt),a,b);Go out on my screen :                                1,1e-05,1,1e-05,8e-07 1.00001 1.00001 IT'S IMPOSSIBLE !!!!!Somebody have a reazon and a solution for this ???
It's actually far from impossible. %g default precision is 6 decimal places. Therefore, for that precision, both sums should give the same result. Increase your precision with something like %.10g and you will see what you are expecting.

As a proof of concept, the following program

main() {

        double a = 1, b = 1, c = 1e-05, d = 1e-05, e = 8e-07;

        printf("%g+%g=%g %g+%g+%g=%g\n", a, c, a+c, b, d, e, b+d+e);

        printf("%g+%g=%g %g+%g+%g=%.10g\n", a, c, a+c, b, d, e, b+d+e);

}

prints out:

1+1e-05=1.00001 1+1e-05+8e-07=1.00001
1+1e-05=1.00001 1+1e-05+8e-07=1.0000108

-- Felix Hernandez