Pierwiastek stopnia n > 2

0

Jak zrobic funkcję liczącą pierwiastek stopnia > 2:

function Pierwiatek(stopien: Integer; liczba: Real): Real;
begin
  // ?????????????????????????
end;

np.

Pierwiastek(3, 8) // =2
0

Nie pisać bo juz jest - Power().

0

Power to jest potega, i do tego nie we wszysrtkich wersjach Delphi

0
wasyl napisał(a)

Power to jest potega, i do tego nie we wszysrtkich wersjach Delphi

No zgadza się, ale nie wiem czy wiesz że np. pierwiastek z 2 to jest 2 do potęgi 0.5 :d . A co do samej funkcji to jeżeli masz jakieś starsze Delphi to prosze bardzo:

function Power(const Base, Exponent: Extended): Extended;
begin
  if Exponent = 0.0 then
    Result := 1.0               { n**0 = 1 }
  else if (Base = 0.0) and (Exponent > 0.0) then
    Result := 0.0               { 0**n = 0, n > 0 }
  else if (Frac(Exponent) = 0.0) and (Abs(Exponent) <= MaxInt) then
    Result := IntPower(Base, Integer(Trunc(Exponent)))
  else
    Result := Exp(Exponent * Ln(Base))
end;

function IntPower(const Base: Extended; const 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;
0
function Pierw(Podst:double;Sto:integer):double;
begin
  result:= exp( ln(abs(Podst))*(1/Sto));
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1