MMMath.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:7k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Tel.: +0351-8012255 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/mmtools.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 20.01.1998 - 18:00:00 $ =}
- {========================================================================}
- unit MMMath;
- {$I COMPILER.INC}
- interface
- const
- {$IFDEF BCB} {$EXTERNALSYM M_PI} {$ENDIF}
- M_PI = 3.14159265358979323846;
- {$IFDEF BCB} {$EXTERNALSYM M2_PI} {$ENDIF}
- M2_PI = 2 * M_PI;
- function MinR(a, b: extended): extended;
- function MaxR(a, b: extended): extended;
- function MinMaxR(x, min, max: extended): extended;
- function LimitR(x, min, max: extended): extended;
- function ModR(x,y: extended): extended;
- function ArcCos(x: extended): extended; { IN: |X| <= 1 OUT: [0..PI] radians }
- function ArcSin(x: extended): extended; { IN: |X| <= 1 OUT: [-PI/2..PI/2] radians }
- function ArcTan2(y, x: extended): extended;
- function Tan(x: extended): extended;
- function Cotan(x: extended): extended; { 1 / tan(X), X <> 0 }
- function Ceil(x: extended): longint;
- function Floor(x: extended): Longint;
- function Log(x: extended): extended;
- function Log2(x: extended): extended;
- function Log10(x: extended): extended;
- function Pow(x,n: extended): extended;
- implementation
- {-------------------------------------------------------------------------}
- { MinR: Returns the smallest signed value }
- function MinR(a, b: extended): extended;
- begin
- if a < b then Result := a
- else Result := b;
- end;
- {-------------------------------------------------------------------------}
- { MaxR: Returns the largest signed value }
- function MaxR(a, b: extended): extended;
- begin
- if a > b then Result := a
- else Result := b;
- end;
- {-------------------------------------------------------------------------}
- { MinMaxR: Returns a value between Min and Max }
- function MinMaxR(x, min, max: extended): extended;
- begin
- if x < min then Result := min
- else if x > max then Result := max
- else Result := x;
- end;
- {-------------------------------------------------------------------------}
- function LimitR(x, min, max: extended): extended;
- begin
- if (max >= min) then
- begin
- if (x < min) then x := min
- else if (x > max) then x := max;
- end
- else
- begin
- if (x < max) then x := max
- else if (x > min) then x := min;
- end;
- Result := x;
- end;
- {-------------------------------------------------------------------------}
- { ModR: Real modulo }
- function ModR(x,y: extended): extended;
- begin
- Result := x - int(x/y)*y;
- end;
- {-------------------------------------------------------------------------}
- { Ceil: Smallest integer >= X, |X| < MaxInt }
- function Ceil(x: extended): longint;
- begin
- Result := Trunc(x);
- if Frac(x) > 0 then inc(Result);
- end;
- {-------------------------------------------------------------------------}
- { Floor: Largest integer <= X, |X| < MaxInt }
- function Floor(x: extended): longint;
- begin
- Result := Trunc(x);
- if Frac(x) < 0 then dec(Result);
- end;
- {-------------------------------------------------------------------------}
- function ArcCos(x: extended): extended;
- begin
- Result := ArcTan2(Sqrt(1 - x*x), x);
- end;
- {-------------------------------------------------------------------------}
- function ArcSin(x: extended): extended;
- begin
- Result := ArcTan2(x, Sqrt(1 - x*x))
- end;
- {-------------------------------------------------------------------------}
- function ArcTan2(y, x: extended): extended; assembler;
- asm
- fld y
- fld x
- fpatan
- fwait
- end;
- {-------------------------------------------------------------------------}
- function Tan(x: extended): extended; assembler;
- { Tan := Sin(X) / Cos(X) }
- asm
- fld x
- fptan
- fstp st(0) { FPTAN pushes 1.0 after result }
- fwait
- end;
- {-------------------------------------------------------------------------}
- function CoTan(x: extended): extended; assembler;
- { CoTan := Cos(X) / Sin(X) = 1 / Tan(X) }
- asm
- fld x
- fptan
- fdivrp
- fwait
- end;
- {-------------------------------------------------------------------------}
- function Log(x: extended): extended; assembler;
- asm
- fld1
- fld x
- fyl2x
- fldl2e
- fdivp st(1),st
- fwait
- end;
- {-------------------------------------------------------------------------}
- function Log2(X: Extended): Extended; assembler;
- asm
- fld1
- fld x
- fyl2x
- fwait
- end;
- {-------------------------------------------------------------------------}
- function Log10(x: extended): extended; assembler;
- asm
- fldlg2 { Log base ten of 2 }
- fld x
- fyl2x
- fwait
- end;
- {-------------------------------------------------------------------------}
- function Pow(x,n: extended): extended; assembler;
- asm
- fld n
- fld x
- fyl2x
- fld st(0)
- frndint
- fld st(0)
- fsubp st(2), st
- fxch st(1)
- f2xm1
- fld1
- faddp st(1), st
- fscale
- fstp st(1)
- end;
- end.