(*********************************************)
(* Name: Math_Routines                       *)
(* Last Revision: 04 jun. 2000               *)
(* Version: 1.2                              *)
(*                                           *)
(* Copyright © 2000 - Christian Aaangel      *)
(*********************************************)

unit Math_Routines;

interface

Uses
 SysUtils;

Const
 Sep = ',';

Function Math_Routines_Version: String;
Function Math_Routines_Date: String;
Function Deci(X: Real; Digit: Byte): Real;
Function EE(E: Integer): Real;

implementation

Function Math_Routines_Version: String;
Begin
 Result := '1.2';
End;

Function Math_Routines_Date: String;
Begin
 Result := '04 jun. 2000';
End;

Function CountFilt(Fra: String; Sepera: Char): Integer;
var
 Res, IC: Integer;
Begin
 Res := 0;
 If Length(Fra) > 0 Then begin
  Res := 1;
  For IC := 1 to length(Fra) do begin
   If fra[IC] = Sepera Then Inc(Res);
  End;
 End;
 Result := Res;
End;

Function GetFilt(Fra: String; Nummer: Integer; Sepera: Char): String;
Var
 LastRecorded, CStart, CSlut, ATF, IC: Integer;
 Res: String;
Begin
 Res := '';
 If Length(Fra) > 0 Then begin
  CStart := 0;
  CSlut := 0;
  ATF := 1;
  LastRecorded := 0;
  If Nummer = 1 Then CStart := 1;
  For IC := 1 to length(Fra) do begin
   LastRecorded := IC;
   If Fra[IC] = Sepera Then begin
    Inc(ATF);
    If ATF = Nummer Then CStart := IC + 1;
    If ATF = Nummer + 1 Then CSlut := IC - 1;
   End;
  End;
  If (CStart <> 0) And (CSlut = 0) Then CSlut := LastRecorded;
  Res := Copy(Fra, CStart, CSlut - CStart + 1);
 End;
 Result := Res;
End;

Function Deci(X: Real; Digit: Byte): Real;
Var
 Before, After, Temp: String;
 I1, Factor: Integer;
 Res: Real;
Begin
 Res := X;
 If Countfilt(Floattostr(x), Sep) > 1 Then begin
  Before := Getfilt(FloatToStr(X), 1, Sep);
  After := GetFilt(FloattoStr(X), 2, Sep);
  If Length(After) > Digit Then begin
   Factor := StrToInt(After[Digit+1]);
   If Factor >= 5 Then begin
    After := Copy(After, 1, Digit+1);
    X := StrToFloat(Before + Sep + After);
    Temp := '0' + Sep;
    For I1 := 1 to digit do Temp := Temp + '0';
    Temp := Temp + FloatToStr(10 - Factor);
    Res := X + StrToFloat(Temp);
{    Neg :=}
   End;
   If Factor < 5 Then Begin
    After := Copy(After, 1, Digit);
    X := StrToFloat(Before + Sep + After);
    Res := X;
   End;
  End;
 End;
 Result := Res;
End;

Function EE(E: Integer): Real;
Var
 S: String;
 I: Integer;
Begin
 S := '1';
 For I := 1 to E do S := S + '0';
 Result := StrToFloat(S);
End;

end.
