2013-06-14 4 views

답변

10

델파이 4 이상에서는 수학 단위의 힘 함수를 사용하십시오. 이전 버전에서는 아래에 나와있는 Pow 함수를 사용합니다.

{** A power function from Jack Lyle. Said to be more powerful than the 
Pow function that comes with Delphi. } 
function Power2(Base, Exponent : Double) : Double; 
{ raises the base to the exponent } 
CONST 
cTiny = 1e-15; 

VAR 
Power : Double; { Value before sign correction } 

BEGIN 
Power := 0; 
{ Deal with the near zero special cases } 
IF (Abs(Base) < cTiny) THEN BEGIN 
    Base := 0.0; 
END; { IF } 
IF (Abs(Exponent) < cTiny) THEN BEGIN 
    Exponent := 0.0; 
END; { IF } 

{ Deal with the exactly zero cases } 
IF (Base = 0.0) THEN BEGIN 
    Power := 0.0; 
END; { IF } 
IF (Exponent = 0.0) THEN BEGIN 
    Power := 1.0; 
END; { IF } 

{ Cover everything else } 
IF ((Base < 0) AND (Exponent < 0)) THEN 
    Power := 1/Exp(-Exponent*Ln(-Base)) 
ELSE IF ((Base < 0) AND (Exponent >= 0)) THEN 
    Power := Exp(Exponent*Ln(-Base)) 
ELSE IF ((Base > 0) AND (Exponent < 0)) THEN 
    Power := 1/Exp(-Exponent*Ln(Base)) 
ELSE IF ((Base > 0) AND (Exponent >= 0)) THEN 
    Power := Exp(Exponent*Ln(Base)); 

{ Correct the sign } 
IF ((Base < 0) AND (Frac(Exponent/2.0) <> 0.0)) THEN 
    Result := -Power 
ELSE 
    Result := Power; 
END; { FUNCTION Pow } 

출처 :

here

+0

이 수학 단위는 어떻게 가져올 수 있습니까? – diegoaguilar

+6

@Diego - uses 절에 추가하십시오. – Shambhala

관련 문제