Calculating the value of e: Difference between revisions

→‎AWK: simplify
(→‎bc: add)
(→‎AWK: simplify)
Line 412:
{{out}}
<pre>The value of e = 2.71828182845905</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">BEGIN {
for (e = n = rfact = 1; rfact >= 1e-15; rfact /= ++n)
# syntax: GAWK -f CALCULATING_THE_VALUE_OF_E.AWK
e += 1.0 / factrfact
BEGIN {
epsilonprintf "e = 1%.0e-1515f\n", e
}</syntaxhighlight>
fact = 1
e = 2.0
n = 2
do {
e0 = e
fact *= n++
e += 1.0 / fact
} while (abs(e-e0) >= epsilon)
printf("e=%.15f\n",e)
exit(0)
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
<pre>
 
e=2.718281828459046
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
Line 557 ⟶ 545:
=={{header|bc}}==
<syntaxhighlight lang="bc">scale = 64
for (e = n = rff = 1; rff != 0; rff /= ++n += 1) e += rff
e</syntaxhighlight>
{{out}}
Line 4,030 ⟶ 4,018:
 
e=0 n=0 rfct=$one
while [ $((rfct /= (n += 1))) -ne 0 ]
do
e=$((e + rfct))
559

edits