Rollo62 536 Posted June 11, 2021 (edited) Hi there, I just wanted to use MulDiv, and my initial reflex was just take it , but: It seems not to be there in Systems, Math or the other, only in Winapi.Windows is the Windows version. I would bet my left finger that MulDiv should be there, am I missing something. The only thing I can find, from older messages, like this: function MathRound(AValue: Extended): Int64; inline; begin if AValue >= 0 then Result := Trunc(AValue + 0.5) else Result := Trunc(AValue - 0.5); end; function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer; begin if nDenominator = 0 then Result := -1 else Result := MathRound(Int64(nNumber) * Int64(nNumerator) / nDenominator); end; That looks a way over the top to me, why do they need floating point ? I will use it like this: function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer; begin if nDenominator = 0 then Result := -1 else Result := (Int64(nNumber) * Int64(nNumerator)) div nDenominator); end; But much better if someone can tell me where can I find a cross-platform, optimized version in the RTL ? Edited June 11, 2021 by Rollo62 1 Share this post Link to post
Stefan Glienke 2002 Posted June 11, 2021 (edited) http://docwiki.embarcadero.com/Libraries/Sydney/en/System.MulDivInt64 - since all platforms apart from win32 are 64bit anyway no need to worry Int64 might be overkill. Edited June 11, 2021 by Stefan Glienke Share this post Link to post
Uwe Raabe 2057 Posted June 11, 2021 DIV and round(/) will give different results depending on input. Share this post Link to post
Rollo62 536 Posted June 12, 2021 15 hours ago, Stefan Glienke said: http://docwiki.embarcadero.com/Libraries/Sydney/en/System.MulDivInt64 - since all platforms apart from win32 are 64bit anyway no need to worry Int64 might be overkill. Ok, thanks. But a simple MulDiv for Integer 32-Bit ? Nothing pops up. At least they are using the same method than I do for Int32. MulDiv64 := Int64((Int128(AValue) * Int128(AMul)) div Int128(ADiv)). Share this post Link to post
David Heffernan 2345 Posted June 12, 2021 Whatever you do, don't use floating point arithmetic! Share this post Link to post