Double equals 0 problem in C

前端 未结 5 1940
执笔经年
执笔经年 2021-01-11 17:56

I was implementing an algorithm to calculate natural logs in C.

double taylor_ln(int z) {
    double sum = 0.0;
    double tmp = 1.0;

    int i = 1;
    whi         


        
5条回答
  •  伪装坚强ぢ
    2021-01-11 18:39

    The print statement is displaying a rounded value, it is not printing the highest possible precision. So your loop has not really reached zero yet.

    (And, as others have mentioned, due to rounding issues it might actually never reach it. Comparing the value against a small limit is therefore more robust than comparing for equality with 0.0.)

提交回复
热议问题