Guest User

Pi

a guest
Dec 12th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. @if defined talk (echo on) else (echo off)
  2. setlocal EnableDelayedExpansion
  3. echo.pi.bat - By Mare
  4. set /a NumQuads = 30
  5. set /a MaxQuadIndex = NumQuads - 1
  6.  
  7. echo.
  8. echo.%time% - started
  9. echo.
  10.  
  11. call :PiEngine 48 18 32 57 -20 239
  12. call :PiEngine 16 5 -4 239
  13. goto :EOF
  14.  
  15. :PiEngine
  16. call :SetToInteger Pi 0
  17. set Formula=
  18. :PiTermLoop
  19. if "%1" == "" (
  20. call :Print pi
  21. echo.
  22. echo.!time! - finished !Formula!
  23. echo.
  24. goto :EOF
  25. )
  26. call :ArctanRecip PiTerm %2
  27. set /a PiEngineFactor=%1
  28. if !PiEngineFactor! lss 0 (
  29. set /a PiEngineFactor *= -1
  30. set Formula=!Formula!
  31. call :MultiplyByInteger PiTerm !PiEngineFactor!
  32. call :Subtract Pi PiTerm
  33. set Operator=-
  34. ) else (
  35. call :MultiplyByInteger PiTerm %1
  36. call :Add Pi PiTerm
  37. set Operator=+
  38. )
  39. if defined Formula (
  40. set Formula=!Formula! !Operator! !PiEngineFactor!*arctan^(1/%2^)
  41. ) else (
  42. set Formula=pi = %1*arctan^(1/%2^)
  43. )
  44. shift
  45. shift
  46. goto PiTermLoop
  47.  
  48. :SetToInteger
  49. for /L %%i in (0, 1, !MaxQuadIndex!) do (
  50. set /a %1_%%i = 0
  51. )
  52. set /a %1_!MaxQuadIndex! = %2
  53. goto :EOF
  54.  
  55. :Print
  56. set PrintBuffer=x
  57. REM Omit a couple of least significant quads, because they will have roundoff errors.
  58. if defined PiDebug (
  59. set /a PrintMinQuadIndex=0
  60. ) else (
  61. set /a PrintMinQuadIndex=2
  62. )
  63. set /a PrintMaxQuadIndex = MaxQuadIndex - 1
  64. for /L %%i in (!PrintMinQuadIndex!, 1, !PrintMaxQuadIndex!) do (
  65. set PrintDigit=!%1_%%i!
  66. if !PrintDigit! lss 1000 (
  67. if !PrintDigit! lss 100 (
  68. if !PrintDigit! lss 10 (
  69. set PrintDigit=000!PrintDigit!
  70. ) else (
  71. set PrintDigit=00!PrintDigit!
  72. )
  73. ) else (
  74. set PrintDigit=0!PrintDigit!
  75. )
  76. )
  77. set PrintBuffer=!PrintDigit!!PrintBuffer!
  78. )
  79. set PrintBuffer=!%1_%MaxQuadIndex%!.!PrintBuffer:x=!
  80. echo.%1 = !PrintBuffer!
  81. goto :EOF
  82.  
  83. :DivideByInteger
  84. if defined PiDebug echo.DivideByInteger %1 %2
  85. set /a DBI_Carry = 0
  86. for /L %%i in (!MaxQuadIndex!, -1, 0) do (
  87. set /a DBI_Digit = DBI_Carry*10000 + %1_%%i
  88. set /a DBI_Carry = DBI_Digit %% %2
  89. set /a %1_%%i = DBI_Digit / %2
  90. )
  91. goto :EOF
  92.  
  93. :MultiplyByInteger
  94. if defined PiDebug echo.MultiplyByInteger %1 %2
  95. set /a MBI_Carry = 0
  96. for /L %%i in (0, 1, !MaxQuadIndex!) do (
  97. set /a MBI_Digit = %1_%%i * %2 + MBI_Carry
  98. set /a %1_%%i = MBI_Digit %% 10000
  99. set /a MBI_Carry = MBI_Digit / 10000
  100. )
  101. goto :EOF
  102.  
  103. :ArctanRecip
  104. if defined PiDebug echo.ArctanRecip %1 %2
  105. call :SetToInteger %1 1
  106. call :DivideByInteger %1 %2
  107. call :CopyValue AR_Recip %1
  108. set /a AR_Toggle = -1
  109. set /a AR_K = 3
  110. :ArctanLoop
  111. if defined PiDebug (
  112. echo.
  113. echo.ArctanRecip AR_K=!AR_K! ---------------------------------------------------------
  114. )
  115. call :DivideByInteger AR_Recip %2
  116. call :DivideByInteger AR_Recip %2
  117. call :CopyValue AR_Term AR_Recip
  118. call :DivideByInteger AR_Term !AR_K!
  119. call :CopyValue AR_PrevSum %1
  120. if !AR_Toggle! lss 0 (
  121. call :Subtract %1 AR_Term
  122. ) else (
  123. call :Add %1 AR_Term
  124. )
  125. call :Compare AR_EqualFlag %1 AR_PrevSum
  126. if !AR_EqualFlag! == true goto :EOF
  127. set /a AR_K += 2
  128. set /a AR_Toggle *= -1
  129. goto ArctanLoop
  130.  
  131. :CopyValue
  132. if defined PiDebug echo.CopyValue %1 %2
  133. for /L %%i in (0, 1, !MaxQuadIndex!) do (
  134. set /a %1_%%i = %2_%%i
  135. )
  136. goto :EOF
  137.  
  138. :Add
  139. if defined PiDebug echo.Add %1 %2
  140. if defined PiDebug call :Print %1
  141. if defined PiDebug call :Print %2
  142. set /a Add_Carry = 0
  143. for /L %%i in (0, 1, !MaxQuadIndex!) do (
  144. set /a Add_Digit = Add_Carry + %1_%%i + %2_%%i
  145. set /a %1_%%i = Add_Digit %% 10000
  146. set /a Add_Carry = Add_Digit / 10000
  147. )
  148. goto :EOF
  149.  
  150. :Subtract
  151. if defined PiDebug echo.Subtract %1 %2
  152. if defined PiDebug call :Print %1
  153. if defined PiDebug call :Print %2
  154. set /a Subtract_Borrow = 0
  155. for /L %%i in (0, 1, !MaxQuadIndex!) do (
  156. set /a Subtract_Digit = %1_%%i - %2_%%i - Subtract_Borrow
  157. if !Subtract_Digit! lss 0 (
  158. set /a Subtract_Digit += 10000
  159. set /a Subtract_Borrow = 1
  160. ) else (
  161. set /a Subtract_Borrow = 0
  162. )
  163. set /a %1_%%i = Subtract_Digit
  164. )
  165. goto :EOF
  166.  
  167. :Compare
  168. if defined PiDebug echo.Compare %1 %2 %3
  169. if defined PiDebug call :Print %2
  170. if defined PiDebug call :Print %3
  171. set /a Compare_Index = 0
  172. set %1=true
  173. :CompareLoop
  174. if not !%2_%Compare_Index%! == !%3_%Compare_Index%! (
  175. if defined PiDebug echo.!%2_%Compare_Index%! neq !%3_%Compare_Index%!
  176. set %1=false
  177. goto :EOF
  178. )
  179. set /a Compare_Index += 1
  180. if !Compare_Index! gtr !MaxQuadIndex! (
  181. if defined PiDebug echo.Compare equal
  182. goto :EOF
  183. )
  184. goto CompareLoop
  185.  
  186. REM $Log: pi.bat,v $
  187. REM
  188. REM Added time stamps and display of formula.
  189. REM
  190. REM
  191. REM Batch file for calculating pi
  192. REM
Advertisement
Add Comment
Please, Sign In to add comment