backend/native: Calculate refresh rate in double-precision

Merged 小田喜陽彦 requested to merge akihiko.odaki/mutter:rate into master

The old calculation was introduced to improve the precision with commit c16a5ec1.

Here, I call the calculation as "revision 2", and the calculation even older as "revision 1", and the new calculation introduced with this commit as "reivion 3".

Revision 2 has two problems:

  1. The calculation is mixed with fixed-point numbers and floating-point numbers.

To overcome the precision loss of fixed-point numbers division, it first "calculates refresh rate in milliHz first for extra precision", but this requires converting the value back to Hz. An extra calculation has performance and precision costs. It is also hard to understand for programmers.

  1. The calculation has a mysterious bias.

In the process, it does: refresh += (drm_mode->vtotal / 2); It is unknown where it came from. Revision 2 did not have it. The value will later divided with drm_mode->vtotal and 1000.0, so the resulting value will always be 0.0005. The number is too small to make any difference, and probably it just consumes some fraction bits.

Revision 3, introduced with this commit always uses double-precision floating-point values for true precision and to ease understanding of this code. It also removes the unknown bias.

Another change is that it now has two internal values, numerator and denominator. Revision 1 also calculated those two values first, and later performed a division with them, which minimizes the precision loss caused by divisions. This method has risks of overflowing the two values and revision 1 caused problems due to that, but revision 3 won't thanks to double-precision. Therefore, revision 3 will theoretically have the result identical with the calculation with infinite-precision.

Merge request reports