开发者

float number and usleep problem

Im wrinting app which has that piece of code, where t->tick is float:

usleep(1000);
t->tick = t->tick + 0.001;
printf("tick is %f, firing time 开发者_高级运维is %f\n", t->tick, t->firing_time);

i found that there is error in usleep?:

tick is 0.313000, firing time is 2.000000
tick is 0.314000, firing time is 2.000000
tick is 0.314999, firing time is 2.000000
tick is 0.315999, firing time is 2.000000

How to get rid of that error ?


There's no error, you simply do not understand how binary floating-point math works.


Looks like a rounding stability error in printf.

Try: printf("tick is %.3f, firing time is %.3f\n", t->tick, t->firing_time);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜