MathUtil: Add Rational class template. #14014
Open
+486
−34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 toRational(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 aRational(6, 2)
which can then manually be.Reduced()
toRational(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 usesstd::frexp
/std::ldexp
which are not actuallyconstexpr
until C++23.