Calculating the value of e: Difference between revisions

Content added Content deleted
(→‎OCaml: add)
(→‎UNIX Shell: add (syntaxhighlight broken, seems to assume a here-doc))
Line 3,955: Line 3,955:
{{Out}}
{{Out}}
<pre>2.718281828</pre>
<pre>2.718281828</pre>

=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh"># POSIX requires "signed long" for shell arithmetic, so assume to have
# at least 31 bits available, and do 2:29 bits fixed point arithmetic

prec=29 one=$((1 << prec))

e=$one fact=1 n=0
while [ $fact -lt $one ]
do
e=$((one / (fact *= (n += 1)) + e))
done

# convert to decimal string
n=9 str=$((e >> prec)). mask=$((one + one - 1))
while [ $((n -= 1)) -ne 0 ]
do
str=$str$(((e = ((mask >>= 1) & e) * 5) >> (prec -= 1)))
done

echo "$str"
</syntaxhighlight>
{{out}}
<pre>2.71828182</pre>

=={{header|VBScript}}==
=={{header|VBScript}}==
{{Trans|Python}}
{{Trans|Python}}
Line 3,974: Line 3,999:
Error = 4.44089209850063E-16
Error = 4.44089209850063E-16
Number of iterations = 9</pre>
Number of iterations = 9</pre>

=={{header|Verilog}}==
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">module main;
<syntaxhighlight lang="verilog">module main;