program POWERS ; {$IFDEF WINDOWS} uses WinCrt ; {$ENDIF} var OK : boolean ; procedure RunError(J : byte) { Non-fatal dummy, only for this test } ; begin OK := false ; Write('RunError ':16, J) end {RunError} ; type Xtype = extended ; Float = single ; function ExpFixed(const X : Xtype ; const Y : longint) : Xtype ; {}function ExpNum(const A : Xtype ; const B : longint) : Xtype ; {}begin if B=0 then ExpNum := 1.0 else {} if Odd(B) then ExpNum := ExpNum(A, B-1) * A {} else ExpNum := Sqr(ExpNum(A, B div 2)) ; {}end {ExpNum} ; begin case X=0.0 of true : if Y>0 then ExpFixed := 0.0 else if Y=0 then ExpFixed := 1.0 else begin ExpFixed := 0.0 ; RunError(200) { (1/X)^Y } end ; false : if Y<0 then ExpFixed := ExpNum(1.0/X, -Y) {} else ExpFixed := ExpNum( X, +Y) ; end {case} ; end {ExpFixed} ; function ExpFloat(const X, Y : Float) : Float { X^Y } ; begin if X>0.0 then begin ExpFloat := Exp(Y*Ln(X)) ; EXIT end ; if X<0.0 then begin ExpFloat := 0.0 ; RunError(234) { or whatever } ; EXIT end ; if Y>0.0 then ExpFloat := 0.0 else if Y=0.0 then ExpFloat := 1.0 else begin ExpFloat := 0.0 ; RunError(200) { (1/X)^Y } end ; end {ExpFloat} ; procedure Its(Q : extended) ; begin if OK then begin Write(' = ') ; if Q=0.0 then Write(Q:11:1) else if (Abs(Q)>1.0E10) or (Abs(Q)<1.0E-3) then Write(Q:16) else Write(Q:16:6) ; end ; Writeln end {Its} ; procedure Test(A, B : extended) ; var J : longint ; begin J := Round(B) ; Write('':4, 'Fixed: ', '(', A:7:3, ' ^ ', J:8, ')') ; OK := true ; Its(ExpFixed(A, J)) ; Write('':4, 'Float: ', '(', A:7:3, ' ^ ', B:8:3, ')') ; OK := true ; Its(ExpFloat(A, B)) ; end {Test} ; const Use : array [0..8] of array [0..1] of extended = ( (+4.001, +3.001), (+4.001, 0.000), (+4.001, -3.001), ( 0.000, +3.001), ( 0.000, 0.000), ( 0.000, -3.001), (-4.001, +3.001), (-4.001, 0.000), (-4.001, -3.001) ) ; var A, B : extended ; K : Low(Use) .. High(Use) ; BEGIN ; Writeln(^M^J'POWERS.PAS (Fixed & Float) : ^C to end.') ; for K := Low(Use) to High(Use) do Test(Use[k][0], Use[K][1]) ; repeat Write(' A^B : A, B ? ') ; Readln(A, B) ; Test(A, B) ; until false ; END.