2 import iam_sympy_example
4 from sympy
.numerics
import *
5 from sympy
.numerics
.utils_
import *
6 from sympy
.numerics
.constants
import pi_float
10 def display_fraction(digits
, skip
=0, colwidth
=10, columns
=5):
11 perline
= colwidth
* columns
13 for linecount
in range((len(digits
)-skip
) // (colwidth
* columns
)):
14 line
= digits
[skip
+linecount
*perline
:skip
+(linecount
+1)*perline
]
15 for i
in range(columns
):
16 print line
[i
*colwidth
: (i
+1)*colwidth
],
17 print ":", (linecount
+1)*perline
18 if (linecount
+1) % 10 == 0:
20 printed
+= colwidth
*columns
21 rem
= (len(digits
)-skip
) % (colwidth
* columns
)
25 for i
in range(columns
):
26 s
+= buf
[:colwidth
].ljust(colwidth
+1, " ")
28 print s
+ ":", printed
+ colwidth
*columns
30 def calculateit(func
, base
, n
, tofile
):
32 intpart
= small_numeral(int(float(func())), base
)
37 Float
.setprec(int(n
*math
.log(base
,2))+10)
38 print "Step 1 of 2: calculating binary value..."
41 step1_time
= clock() - t
42 print "Step 2 of 2: converting to specified base..."
44 d
= bin_to_radix(a
.man
, -a
.exp
, base
, n
)
45 d
= fixed_to_str(d
, base
, n
)
46 step2_time
= clock() - t
47 print "\nWriting output...\n"
51 print "%i base-%i digits of pi:\n" % (n
, base
)
53 display_fraction(d
, skip
, colwidth
=10, columns
=5)
56 print "\nFinished in %f seconds (%f calc, %f convert)" % \
57 ((step1_time
+ step2_time
), step1_time
, step2_time
)
60 print "Compute digits of pi with SymPy\n"
61 base
= input("Which base? (2-36, 10 for decimal) \n> ")
62 digits
= input("How many digits? (enter a big number, say, 10000)\n> ")
63 tofile
= raw_input("Output to file? (enter a filename, or just press enter\nto print directly to the screen) \n> ")
65 tofile
= open(tofile
, "w")
66 global_options
["verbose"] = True
67 global_options
["verbose_base"] = base
68 calculateit(pi_float
, base
, digits
, tofile
)
69 raw_input("\nPress enter to close this script.")