home *** CD-ROM | disk | FTP | other *** search
- {XTON.INC}
-
- {
- This include file contains the x_to_n function that computes
- the Nth power of a real number, where N is an integer.
-
- Source: "Raising X To The Nth Degree", TUG Lines Volume I Issue 5
- Author: Herb Holden
- Date: 11/17/84
- Application: All systems
- }
-
- FUNCTION xton(x:real ; n:integer):real ;
-
- var p:real ;
-
- begin { computes x to the nth power }
- if x=0
- then begin
- if n=0
- then begin { 0 to the 0 is undefined }
- writeln('** error in xton: x=0 n=0 **') ;
- xton:=1/0
- end
- else xton:=0 { 0 to the n is 0 }
- end
-
- else begin
- if n<0 { x to -n is (1/x) to n }
- then begin x:=1/x ; n:=-n end ;
-
- if odd(n) { prepare for while loop }
- then p:=x
- else p:=1 ; { x to 0 will be 1 }
- n:=n shr 1 ;
-
- while n<>0 do
- begin
- x:=sqr(x) ;
- if odd(n) then p:=p*x ;
- n:=n shr 1
- end ;
- xton:=p
- end
- end ; { *** end of xton *** }