Is there a MulDiv function for cross platform usage?
Asked Answered
L

1

5

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 ?

Laroy answered 25/2, 2013 at 3:49 Comment(0)
R
8

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.

Rora answered 25/2, 2013 at 11:33 Comment(1)
I have a feeling Java (I am doing Android stuff) makes something like MulDiv (would be quite coherent to the Java culture). I have similar print pixel calculations in Android Java as in Win32 C and I get the same results using MulDiv in Win32 C, else not (I like the printed pages to look about the same). How is Java making such calculations, would be interesting to know? In fact if anyone made a study of different platforms/programming languages It would be interesting to read?Fleabitten

© 2022 - 2024 — McMap. All rights reserved.