Jump to content
Sign in to follow this  
Rollo62

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
  • Like 1

Share this post


Link to post
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×