Skip to content

Small deviations in timers #3628

@Alex-1248

Description

@Alex-1248

Mc 1.7.10
OpenComputers-MC1.7.10-1.8.2+94f6405-universal

I ran a program that kept repeating os.sleep(0.05) and noticed that at some times it sleeps for 0.05 seconds and at other times the sleep time fluctuates between 0.05 and 0.1 seconds. This happens systematically and predictably, and is related to the current value of computer.uptime(). For example, if you run such a program immediately after starting the computer, then fluctuations in sleep time begin at 8 seconds and end at about 30 seconds, and then start again at 128 seconds. If you assign 0.049 or 0.025 as sleep time, then the fluctuations almost disappear. If you replace os.sleep() with computer.pullSignal(), the results are the same. It seems that the fluctuations are caused by the inability to accurately represent most decimal fractions in the binary system and the resulting errors in arithmetic operations. Because of this, sometimes when a wake-up should occur, the deadline is slightly higher than computer.uptime() and the sleep is extended by 1 game tick. Event.timer() can theoretically also sometimes call a function a little later than it should. And also, if I understand the implementation of event.timer() correctly, it can drift if a number that does not have an exact binary representation is chosen as the interval, although a significant shift requires a large number of cycles and a large computer.uptime() value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugOC feature is broken

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions