atan provides a handy way to ask Tcl for the value of pi:
% expr {atan(1) * 4} 3.1415926535897931
MGS Actually, using acos() is (slightly) more efficient:
% set tcl_precision 17 17 % expr {acos(-1)} 3.1415926535897931
Does anyone have any data on which method is preferable from a numerical point of view?
IDG Both contain the assumption that the transcendental functions are accurate to the last ulp. In many math libraries this is not so. I think you are safer with a string representation:
set pi 3.1415926535897931
GS (030927) Here is a small program able to compute 2400 digits of pi:
# pi-2400.tcl # 2400 digits of pi with a spigot algorithm set e 0 for {set b 0} {$b <= 8400} {incr b} {set f($b) 2000} for {set c 8400} {$c > 0} {incr c -14} { set d 0 for {set b $c} {$b > 0} {incr b -1} { set g [expr 2*$b -1] set d [expr ($d*$b) + ($f($b)*10000)] set f($b) [expr round([expr fmod($d,$g)])] set d [expr $d/$g] } puts -nonewline [format "%.4i" [expr $e+($d/10000)]] flush stdout set e [expr round([expr fmod($d,10000)])] }
It uses a spigot algorithm. More detail in A spigot algorithm for the digits of pi, Stanley Rabinowitz and Stan Wagon, American Mathematical Monthly, March 1995, pp195-203.
Math function help - Arts and Crafts of Tcl-Tk Programming - Category Mathematics