0

I was looking for the definition in Extended Pascal for the ** exponentiation operation. I've been looking for awhile now and can't seem to find it.

i.e 2**3 = 8
thesentyclimate413
  • 89
  • 3
  • 4
  • 11
  • What do you mean by definition? It's called exponentiation and is probably implemented using the square-and-multiply approach or some variant of that. – Thomas Mar 13 '13 at 04:09
  • Most Pascal implementations provide standard functions for Exp, Ln and Power. For example: http://www.freepascal.org/docs-html/rtl/math/power.html – paulsm4 Mar 13 '13 at 04:09
  • I was looking for how Pascal implemented it – thesentyclimate413 Mar 13 '13 at 04:12
  • 1
    Free Pascal does not follow extended Pascal, we just borrowed the syntax when we implemented an exponentiation operator. The Pascal standards afaik still require a fee to obtain, though parts of them have been seen floating on the web (based on publicized bits or drafts?) – Marco van de Voort Mar 13 '13 at 09:27
  • Compilers are free to implement language features however they like. There is no single definition of how "Pascal" implements the operator. Of which compiler were you looking for the operator's definition? – Rob Kennedy Mar 13 '13 at 15:37

1 Answers1

4

In FreePascal it is implemented in the math unit:

operator ** (bas,expo : float) e: float; inline;
  begin
    e:=power(bas,expo);
  end;


operator ** (bas,expo : int64) i: int64; inline;
  begin
    i:=round(intpower(bas,expo));
  end;

function power(base,exponent : float) : float;

  begin
    if Exponent=0.0 then
      result:=1.0
    else if (base=0.0) and (exponent>0.0) then
      result:=0.0
    else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
      result:=intpower(base,trunc(exponent))
    else if base>0.0 then
      result:=exp(exponent * ln (base))
    else
      InvalidArgument;
  end;

function intpower(base : float;const exponent : Integer) : float;

  var
     i : longint;

  begin
     if (base = 0.0) and (exponent = 0) then
       result:=1
     else
       begin
         i:=abs(exponent);
         intpower:=1.0;
         while i>0 do
           begin
              while (i and 1)=0 do
                begin
                   i:=i shr 1;
                   base:=sqr(base);
                end;
              i:=i-1;
              intpower:=intpower*base;
           end;
         if exponent<0 then
           intpower:=1.0/intpower;
       end;
  end;   
BeniBela
  • 16,412
  • 4
  • 45
  • 52