home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / MADTRB11.ZIP / XTON.INC < prev    next >
Encoding:
Text File  |  1985-08-05  |  1.1 KB  |  46 lines

  1. {XTON.INC}
  2.  
  3. {
  4. This include file contains the x_to_n function that computes
  5. the Nth power of a real number, where N is an integer.
  6.  
  7. Source: "Raising X To The Nth Degree", TUG Lines Volume I Issue 5
  8. Author: Herb Holden
  9. Date: 11/17/84
  10. Application: All systems
  11. }
  12.  
  13. FUNCTION xton(x:real ; n:integer):real ;
  14.  
  15. var p:real ;
  16.  
  17. begin   { computes x to the nth power }
  18. if x=0
  19.  then begin
  20.       if n=0
  21.        then begin    { 0 to the 0 is undefined }
  22.             writeln('** error in xton: x=0 n=0 **') ;
  23.             xton:=1/0
  24.             end
  25.        else xton:=0  { 0 to the n is 0 }
  26.       end
  27.  
  28.        else begin
  29.             if n<0   { x to -n is (1/x) to n }
  30.              then begin x:=1/x ; n:=-n end ;
  31.  
  32.             if odd(n)    { prepare for while loop }
  33.              then p:=x
  34.              else p:=1 ; { x to 0 will be 1 }
  35.             n:=n shr 1 ;
  36.  
  37.             while n<>0 do
  38.              begin
  39.              x:=sqr(x) ;
  40.              if odd(n) then p:=p*x ;
  41.              n:=n shr 1
  42.              end ;
  43.             xton:=p
  44.             end
  45.        end ;      { *** end of xton *** }
  46.