Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Negative power...

Status
Not open for further replies.

Matilde

Programmer
Nov 30, 2001
18
0
0
DK
How can you do ie. -5^-4, without using the power function? You can't do the exp(-4*ln(-5)) because ln(var) can not be negative...?

Matilde... ;o)
 
The Math function IntPower allows a negative base and a negative exponent e.g.
Code:
 IntPower(-5, -4)
returns 0.0016. Hope this helps. Clive [infinity]
 
Yes you might also consider McMerys solution over IntPower as as you dont get the 'math.pas' libarary with all versions of Delphi.
However it will be faster as it uses assembler.

Err. Sort of like this..


function IntPower(Base: Extended; Exponent: Integer): Extended;
asm
mov ecx, eax
cdq
fld1 { Result := 1 }
xor eax, edx
sub eax, edx { eax := Abs(Exponent) }
jz @@3
fld Base
jmp @@2
@@1: fmul ST, ST { X := Base * Base }
@@2: shr eax,1
jnc @@1
fmul ST(1),ST { Result := Result * X }
jnz @@1
fstp st { pop X from FPU stack }
cmp ecx, 0
jge @@3
fld1
fdivrp { Result := 1 / Result }
@@3:
fwait
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top