Need code for Inverse Error Function

前端 未结 7 1877
清歌不尽
清歌不尽 2021-02-10 00:54

Does anyone know where I could find code for the \"Inverse Error Function?\" Freepascal/Delphi would be preferable but C/C++ would be fine too.

The TMath/DMath library d

7条回答
  •  执念已碎
    2021-02-10 01:37

    function erf(const x: extended): extended;
    var
      n: integer;
      z: extended;
    begin
      Result := x;
      z := x;
      n := 0;
    
      repeat
        inc(n);
        z := -z * x * x * (2 * n - 1) / ((2 * n + 1) * n);
        Result := Result + z;
      until abs(z) < 1E-20;
    
      Result := Result * 2 / sqrt(pi);
    end;
    
    function erfinv(const x: extended): extended;
    var
      n: integer;
      z: extended;
    begin
      Result := 0;
      n := 0;
    
      repeat
        inc(n);
        z := (erf(Result) - x) * sqrt(pi) / (2 * exp(-Result * Result));
        Result := Result - z;
      until (n = 100) or (abs(z) < 1E-20);
    
      if abs(z) < 1E-20 then
        n := -20
      else
        n := Floor(Log10(abs(z))) + 1;
    
      Result := RoundTo(Result, n);
    end;
    

提交回复
热议问题