Floating point timers and round-off error

Regarding Sam’s statement that we use update to decrement timers rather than put a “time to end” in the dictionary and compare with the unity game timer because of floating point issues:

I saw it somewhere, might have even been one of the Gamedev.TV courses, a “Thought Question”:

Game runs fine for days but after 13 days, it crashes. Why?

The point was, only after about 13 days does the “exponent” on the float get high enough that the smallest possible time increment (the machine epsilon for that value) is too big to be useful (can’t remember the details, perhaps bigger than a frame interval).

So…if you want to use the timer for time, just shut the game down and restart every 13 days or so!

This wikipedia entry explains how floating points are (usually) stored (I’ve used computers, like older Crays, that have other schemes, but this one is in about every home computer nowadays) IEEE 754 - Wikipedia .

I believe this was in a special lecture not tied to a specific course, but in our Announcements “course”. Here’s the lecture in question:

The crux of the discussion is that we start to lose precision in as little as 1/2 of a second as floats begin to increase by Time.deltaTime. Now this loss of precision is very small in Sam’s example, but there is a loss of precision. The question is how much of that precision can we afford to lose.

For larger time intervals… say our timer is 5 minutes to an hour, you’re not likely to notice this lack of precision. In smaller time intervals, such as coordinating a delayed action with an animation, that loss of precision could mess up the timing, and it won’t take 13 days for that to become noticeable.

In most cases, it’s unlikely to be noticed, but the shorter the timer, the more likely that precision errors can cause noticeable issues.

For the most part, just understand that Sam is just explaining his reasons for using timers the way he does. Time.time + interval timers are used all the time.

Personally, I actually use countdown timers instead of count up- timers like Sam’s example. Just my preference, and it simplifies cooldown displays.

Privacy & Terms