delphidabbler/code-snippets

Add logarithmic mean function

Closed this issue · 2 comments

The logarithmic mean of two +ve numbers (not same as log-mean), is defined as:

Where x = y:

Mlm(x, y) = x

Where x ≠ y:

Mlm(x, y) = (y - x) / (ln(y) - ln(x))

So:

function LogarithmicMean(const X, Y: Double): Double;
begin
  Assert((X > 0) and (Y > 0));
  if SameValue(X, Y) then
    Result := X
  else
    Result := (Y - X) / (Ln(Y) - Ln(X));
end;

This issue was extracted from issue #16

Version with non-negative Integer parameters:

function LogarithmicMean(const X, Y: Cardinal): Double;
begin
  Assert((X > 0) and (Y > 0));
  if X = Y then
    Result := X
  else
    Result := (Y - X) / (Ln(Y) - Ln(X));
end;

Don't create a version with Integer parameters since both parameters must be > 0 anyway.

Added Double parameter version of LogarithmicMean function, but no overloads.

Implemented by merge commit 28b2b6f