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