Skip to content

Instantly share code, notes, and snippets.

@realeroberto
Created September 15, 2018 22:26
Show Gist options
  • Save realeroberto/a68f61a59e1653c07df90ded1446d5f5 to your computer and use it in GitHub Desktop.
Save realeroberto/a68f61a59e1653c07df90ded1446d5f5 to your computer and use it in GitHub Desktop.
Approximate the value of e using the power series expansion
#!/usr/bin/tclsh
#
# approximate e using the power series expansion
#
proc approx_e {{n 20}} {
set value 1
set factorial 1
set i 1
while {$i <= $n} {
set factorial [expr {$factorial * $i}]
set value [expr {$value + 1.0/$factorial}]
incr i
}
return $value
}
puts [approx_e]
# ex: ts=4 sw=4 et filetype=tcl noexpandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment