Loss of precision on adding doubles?

前端 未结 3 715
难免孤独
难免孤独 2021-01-16 00:54

folks! I\'ve encountered a little problem: I\'m doing a simple addition with three double values. The result has a smaller precision than the used values.

d         


        
3条回答
  •  执念已碎
    2021-01-16 01:06

    Sometimes in order to get the value according to the desired precision we have to specify the limit of precision,here is my code which works fine ,hope it helps:

    double minutes = 3;
    minutes = minutes / (24.0*60.0);
    double hours = 3;
    hours = hours / 24.0; 
    double days = 3;
    double age = days + hours + minutes;
    printf("%.8f",age);//here i have included the places of precision (.8)
    

提交回复
热议问题