Error "Invalid floating point operation"

procedure TForm1.Button1Click(Sender: TObject);
var y, yInDegree, EPS, diff, x, yMinus1 : Extended;
    i, n : Cardinal;
begin
  x := StrToFloat(Edit1.Text);
  n := StrToInt(Edit3.Text);
  EPS := StrToFloat(Edit2.Text);
  i := 1;
  y := 1.0;
  yInDegree := 1.0;
  while (diff > EPS) do
    begin
    yMinus1 := y;
    y := ((1/n) * ((n-1) * y + (x/yInDegree)));  // <<<-- Ошибка тут
    yInDegree := exp(n*ln(y));
    diff := y - yMinus1;
    inc(i);
    end;
  Label5.Caption := IntToStr(i);
end;

Error text:

Project Project1.exe raised exception class EInvalidOp with message 'Invalid floating point operation'

The error crashes on the line y := ((1/n) * ((n-1) * y + (x/yInDegree)));.

Author: Nicolas Chabanovsky, 2016-10-05

1 answers

Either n or, more likely, yInDegree take values near zero on the next iteration.

Modify the code

try
   y := ((1/n) * ((n-1) * y + (x/yInDegree)));
except
  ShowMessageFmt('n: %f, y: %f, yInDegree: %d', [n, y, yInDegree]);
end;

And look at the resulting values

 3
Author: Anton Shchyrov, 2016-10-06 07:21:59