# MulDiv( ) : Integer; in Delphi, cannot find this in the RTL (cross-platform)

## Recommended Posts

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 by Rollo62

DIV and round(/) will give different results depending on input.

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)). `

Whatever you do, don't use floating point arithmetic!