浮点数和usleep问题
我正在编写有这段代码的应用程序,其中 t->tick 是 float:
usleep(1000);
t->tick = t->tick + 0.001;
printf("tick is %f, firing time is %f\n", t->tick, t->firing_time);
我发现 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
如何摆脱该错误?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有错误,你只是不明白二进制浮点数学是如何工作的。
There's no error, you simply do not understand how binary floating-point math works.
看起来像 printf 中的舍入稳定性错误。
尝试: printf("tick 为 %.3f,触发时间为 %.3f\n", t->tick, t->firing_time);
Looks like a rounding stability error in printf.
Try: printf("tick is %.3f, firing time is %.3f\n", t->tick, t->firing_time);