Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a MulDiv function for cross platform usage?

Can anyone help me to find a unit, which defines the MulDiv function in Delphi XE3 for cross platform usage ? Its prototype is defined in Windows unit (as usually), what won't obviously work under OSX.

Is there a MulDiv function for cross platform usage in Delphi XE3 ?

like image 359
Martin Avatar asked Feb 25 '13 03:02

Martin


1 Answers

There is no MulDiv function for cross platform usage, there's just the one imported from Windows. So, you'll need to make such function for different platforms by yourself if you need it. For instance Lazarus uses for this the similar code:

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;

Source lcltype.pp unit and issue #0009934.

like image 93
TLama Avatar answered Sep 19 '22 02:09

TLama