Skip to content

Conversation

jordan-woyak
Copy link
Member

This adds a rational number (i.e. "fraction") class template.

This de-uglifies the VideoInterfaceManager::GetTargetRefreshRateNumerator/GetTargetRefreshRateDenominator interface.
In the future, another cleanup opportunity I have eyed up is Mixer's FIXED_SAMPLE_RATE_DIVIDEND.

All the expected operators are defined and can be mixed with float/int on either side.

I've added a bunch of unit tests.

I'm not entirely happy with the verbosity of the float conversion constructor implementation, but it does work, and e.g. 0.5 is precisely converted to Rational(1, 2).

I've elected to make operations not automatically reduce their results for efficiency's sake.
e.g. Rational(2, 1) * Rational(3, 2) will produce a Rational(6, 2) which can then manually be .Reduced() to Rational(3, 1).
My rationale was that, either way, one has to worry about overflow, so it might as well require manual reduction.
But if someone has a strong opinion that results should be automatically reduced, I'm open to that.

The algorithm for Approximated comes from ffmpeg (LGPLv2.1+).

All operations are constexpr except the float conversion constructor which uses std::frexp/std::ldexp which are not actually constexpr until C++23.

@jordan-woyak jordan-woyak force-pushed the mathutil-rational branch 2 times, most recently from 22aafb7 to 160459a Compare October 15, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant