Search code examples
pascalpowexponentiationexp

Implement x^(e) in Extended Pascal, without using exponentiation operators


Part of a Pascal ISO 10206 program I am building, requires me to implement a function to exponentiate a real number (x) to Eulers number (e), without using any exponentiation functions already included in Pascal(**,pow,exp...).

I have been trying different algorithms for hours but I cant figure out how to do it without using the already included exponentiation functions.

Any help would be appreciated. Any mathematical algorithm of some sort etc... Thanks in advance.


Solution

  • I used the solution pointed out by @RudyVelthuis in some other post, but modified it a bit. It is based upon that x^0.5 = sqrt(x), which we can use to our advantage. Ill leave the Pascal ISO 10206 code I used attached. Thank you all for your help, specially Rudy.

    function MyPow(base,power,precision:real):real;
    begin
        if (power<0) then MyPow:=1/MyPow(base,-power,precision)
        else   if (power>=10) then MyPow:=sqr(MyPow(base,power/2,precision/2))
        else   if (power>=1) then MyPow:=base*MyPow(base,power-1,precision)
        else   if (precision>=1) then MyPow:=sqrt(base)
        else MyPow:=sqrt(MyPow(base,power*2,precision*2));
    end;