You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add mult_frac() to handle fractional multiplication safely
Introduce a new inline function mult_frac() to compute "x * n / d" with
enhanced precision and safety. This function addresses the common
issues of overflow and loss of precision that can occur with
straightforward approaches to this calculation.
Directly computing "x * n / d" can lead to problems:
- Performing the division first can result in loss of precision due to
integer truncation.
- Performing multiplication before division can risk overflow.
The mult_frac() function mitigates these issues by:
1. Calculating the quotient and remainder of 'x' divided by 'd'.
2. Using these intermediate results to perform the final calculation,
thus avoiding intermediate overflow and preserving precision.
This approach is based on the Linux kernel's mult_frac() macro [1],
which follows a similar method to handle these calculations robustly.
Link: https://elixir.bootlin.com/linux/v6.10.7/source/include/linux/math.h#L121 [1]
0 commit comments