JosiP
JosiP

Reputation: 3079

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 ?

Upvotes: 0

Views: 534

Answers (2)

zvrba
zvrba

Reputation: 24546

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

Upvotes: 2

ocsid80
ocsid80

Reputation: 111

Looks like a rounding stability error in printf.

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

Upvotes: 0

Related Questions