U
U
uliana002017-11-01 16:10:37
Delphi
uliana00, 2017-11-01 16:10:37

How to raise a number to a negative power without using the Power function?

Calculate the sum of the series with a given degree of accuracy a: Σ(-1)^n*1/(2n+1), with a=0.0001.
Here is the working code. How to raise an integer to a negative power without using power?

uses
  SysUtils, math;
var
  i,sum,eps:Real;
  n:Integer;
begin
  sum:=0;
  eps:=0.0001;
  n:=1;
  i:=0;
  while eps>i do
  begin
    i:=1/(2*n+1)*power(-1, n);
    sum:=sum+i;
    inc(n);
  end;
  Writeln(sum);
  readln;
end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-11-01
@Mercury13

A number to a negative power or a negative number to a power?
A negative number can only be raised to an integer power (it’s not clear from a rational number, but how did we get the concept of “real degree”? - we supplemented the rational by continuity).
Here you need to raise −1 to the power of n, and the easiest way
for this series is to do so. But if we have a series for the sine, which is calculated recurrently, then

x2 := Sqr(x);
...
yNew := -yOld * x2 / ((n - 1) * n);

C
cicatrix, 2017-11-01
@cicatrix

Ah, I remember school. In my opinion, it was in the first lesson in Pascal - raising the number a to the power b:

function MyPower(a,b:real):real;
begin
      Result := exp(b*ln(a));
end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question