Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: pi
base=10000 nblock=10 ;3.1415 9265 3589 7932 3846 2643 3832 7950 2884 1971 6939 9375 1058 2097 4944 ; Pi/4 = 4*arctan(1/5)-arctan(1/239) (Machin) a=TimeYmdhms() gosub pi exit ;;-------------------------------------------------- :pi ;; calcul de pi au moyen de la formule ;; Pi/4 = 4*arctan(1/5)-arctan(1/239) (Machin) ;;-------------------------------------------------- denom=5 gosub arctan for i=0 to nblock xx%i%=result%i% next factor=4 gosub mult for i=0 to nblock rr%i%=xx%i% next denom=239 gosub arctan for i=0 to nblock xx%i%=rr%i% yy%i%=result%i% next gosub sub factor=4 gosub mult for i=0 to nblock result%i% = xx%i% next b=TimeYmdhms() s=TimeDiffSecs(b,a) gosub affiche return ;;-------------------------------------------------- :affiche ;;-------------------------------------------------- msg=strcat(s," s ") msg=strcat(msg,result0," ") for i=1 to nblock-1 ss=result%i% if(result%i% <1000) then ss=strcat("0",result%i%) endif if(result%i% <100) then ss=strcat("00",result%i%) endif if(result%i% <10) then ss=strcat("000",result%i%) endif msg=strcat(msg," ",ss) next message("INFO",msg) return ;;-------------------------------------------------- :zero ; returns 0 if yy != 0 otherwise 1 ;;-------------------------------------------------- retour=1 for i=0 to nblock if(yy%i% != 0) then retour=0 break endif next return ;;-------------------------------------------------- :add ;; xx=xx+yy ;;-------------------------------------------------- for i=nblock-1 to 0 by -1 xx%i%=xx%i% + yy%i% if( xx%i% >= BASE) then xx%i% = xx%i% - BASE j=i-1 xx%j% = xx%j% + 1 endif next return ;;-------------------------------------------------- :sub ;; xx=xx-yy ;;-------------------------------------------------- for i=nblock-1 to 0 by -1 xx%i%=xx%i% - yy%i% if( xx%i% < 0) then xx%i% = xx%i% +BASE j=i-1 xx%j% = xx%j% - 1 endif next return ;;-------------------------------------------------- :mult ;; xx=xx*factor ;;-------------------------------------------------- carry=0 for i=nblock-1 to 0 by -1 xx%i%=xx%i% * factor xx%i%=xx%i% + carry carry=xx%i% / BASE xx%i% = xx%i% mod BASE next return ;;-------------------------------------------------- :div1 ;; xx = xx / denom ;;-------------------------------------------------- carry=0 for i=0 to nblock -1 xx%i%=xx%i% + carry*BASE carry=xx%i% mod denom xx%i% = xx%i% / denom next return ;;-------------------------------------------------- :set ;; set rhs to xx ;;-------------------------------------------------- for i=1 to nblock xx%i%=0 next xx0=rhs return ;;-------------------------------------------------- :arctan ;; compute arctan(1/denom) ;; result in result ;;-------------------------------------------------- retour=0 rhs=1 k=1 gosub set ; x= 1/denom gosub div1 for i=0 to nblock r%i%=xx%i% ;; sauvegarde x**1 result%i% = r%i% ; resultat = x next while retour == 0 for i=0 to nblock xx%i%=r%i% ; restauration x**n next gosub div1 gosub div1 for i=0 to nblock r%i%=xx%i% ; sauvegarde x**n next x=denom denom=2*k + 1 gosub div1 denom=x for i=0 to nblock yy%i%=xx%i% ; x**n/(2k+1) xx%i%=result%i% next if ( k mod 2) then gosub sub else gosub add endif for i=0 to nblock result%i%=xx%i% ; resultat = resultat +/- x**n/(2k+1) next k=k+1 gosub zero endwhile return
#DefineFunction main() base=10000 nblock=10 ;3.1415 9265 3589 7932 3846 2643 3832 7950 2884 1971 6939 9375 1058 2097 4944 ; Pi/4 = 4*arctan(1/5)-arctan(1/239) (Machin) pi(nblock,base) return #EndFunction #DefineFunction pi(nblock,base) ;;-------------------------------------------------- ;; calcul de pi au moyen de la formule ;; Pi/4 = 4*arctan(1/5)-arctan(1/239) (Machin) ;;-------------------------------------------------- a=TimeYmdhms() xx=ArrDimension(nblock+1,0,0,0,0) yy=ArrDimension(nblock+1,0,0,0,0) result=ArrDimension(nblock+1,0,0,0,0) rr=ArrDimension(nblock+1,0,0,0,0) denom=5 arctan(result,denom,nblock,base) for i=0 to nblock xx[i]=result[i] next factor=4 mult(xx,factor,nblock,base) for i=0 to nblock rr[i]=xx[i] next denom=239 arctan(result,denom,nblock,base) for i=0 to nblock xx[i]=rr[i] yy[i]=result[i] next sub(xx,yy,nblock,base) factor=4 mult(xx,factor,nblock,base) for i=0 to nblock result[i] = xx[i] next b=TimeYmdhms() s=TimeDiffSecs(b,a) affiche(result,nblock,base,s) return #EndFunction #DefineFunction affiche(result,nblock,base,s) ;;-------------------------------------------------- msg=strcat(s," s ") msg=strcat(msg,result[0]," ") for i=1 to nblock-1 ss=result[i] if(result[i] <1000) then ss=strcat("0",result[i]) endif if(result[i] <100) then ss=strcat("00",result[i]) endif if(result[i] <10) then ss=strcat("000",result[i]) endif msg=strcat(msg," ",ss) next message("INFO",msg) return 0 #EndFunction #DefineFunction zero(yy,nblock) ;;-------------------------------------------------- ; returns 0 if yy != 0 otherwise 1 ;;-------------------------------------------------- for i=0 to nblock if(yy[i] != 0) then return 0 break endif next return 1 #EndFunction #DefineFunction add(xx,yy,nblock,base) ;;-------------------------------------------------- ;; xx=xx+yy ;;-------------------------------------------------- for i=nblock-1 to 0 by -1 xx[i]=xx[i] + yy[i] if( xx[i] >= BASE) then xx[i] = xx[i] - BASE j=i-1 xx[j] = xx[j] + 1 endif next return #EndFunction #DefineFunction sub(xx,yy,nblock,base) ;;-------------------------------------------------- ;; xx=xx-yy ;;-------------------------------------------------- for i=nblock-1 to 0 by -1 xx[i]=xx[i] - yy[i] if( xx[i] < 0) then xx[i] = xx[i] +BASE j=i-1 xx[j] = xx[j] - 1 endif next return #EndFunction #DefineFunction mult(xx,factor,nblock,base) ;;-------------------------------------------------- ;; xx=xx*factor ;;-------------------------------------------------- carry=0 for i=nblock-1 to 0 by -1 xx[i]=xx[i] * factor xx[i]=xx[i] + carry carry=xx[i] / BASE xx[i] = xx[i] mod BASE next return #EndFunction #DefineFunction div1(xx,denom,nblock,base) ;;-------------------------------------------------- ;; xx = xx / denom ;;-------------------------------------------------- carry=0 for i=0 to nblock -1 xx[i]=xx[i] + carry*BASE carry=xx[i] mod denom xx[i] = xx[i] / denom next return #EndFunction #DefineFunction set(xx,rhs,nblock,base) ;;-------------------------------------------------- ;; set rhs to xx ;;-------------------------------------------------- for i=1 to nblock xx[i]=0 next xx[0]=rhs return #EndFunction #DefineFunction arctan(result,denom,nblock,base) ;;-------------------------------------------------- ;; compute arctan(1/denom) ;; result in result ;;-------------------------------------------------- r1=ArrDimension(nblock+1,0,0,0,0) xx1=ArrDimension(nblock+1,0,0,0,0) yy1=ArrDimension(nblock+1,0,0,0,0) retour=0 rhs=1 k=1 set(xx1,rhs,nblock,base) ; x= 1/denom div1(xx1,denom,nblock,base) for i=0 to nblock r1[i]=xx1[i] ;; sauvegarde x**1 result[i] = r1[i] ; resultat = x next while retour == 0 for i=0 to nblock xx1[i]=r1[i] ; restauration x**n next div1(xx1,denom,nblock,base) div1(xx1,denom,nblock,base) for i=0 to nblock r1[i]=xx1[i] ; sauvegarde x**n next denom1=2*k + 1 div1(xx1,denom1,nblock,base) for i=0 to nblock yy1[i]=xx1[i] ; x**n/(2k+1) xx1[i]=result[i] next if ( k mod 2) then sub(xx1,yy1,nblock,base) else add(xx1,yy1,nblock,base) endif for i=0 to nblock result[i]=xx1[i] ; resultat = resultat +/- x**n/(2k+1) next k=k+1 retour=zero(yy1,nblock) endwhile return #EndFunction main()
Article ID: W14956
File Created: 2001:11:08:12:41:08
Last Updated: 2001:11:08:12:41:08