在Delphi中如何将数字提升为幂?

在Delphi中如何将数字提升为幂?,delphi,Delphi,在Delphi中如何将数字提升为幂?在Delphi 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 th

在Delphi中如何将数字提升为幂?

在Delphi 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 }
{**Jack Lyle提供的电源功能。据说比
Delphi附带的Pow函数。}
函数Power2(基数,指数:Double):Double;
{将基数提升到指数}
常数
cTiny=1e-15;
变量
功率:双倍;{符号校正前的值}
开始
功率:=0;
{处理接近零的特殊情况}
如果(Abs(Base)=0)),则
幂:=Exp(指数*Ln(-Base))
否则如果((基数>0)和(指数<0)),则
幂:=1/Exp(-Exponent*Ln(基))
否则,如果((基数>0)和(指数>=0)),则
幂:=Exp(指数*Ln(基));
{更正符号}
如果((基数<0)和(分形指数/2.0)0.0)),则
结果:=-功率
其他的
结果:=功率;
结束;{函数Pow}
资料来源:


阅读更多

尝试一下,即使是一个简单的谷歌搜索也能告诉你这一点。@Diego。您使用的是哪种Delphi版本?@Diego。你的问题甚至没有包含“这个数学函数不包括在内”的描述。甚至在谷歌搜索你的确切问题标题时也会出现answer@Diego-将其添加到您的uses子句中