Calculating the value of e: Difference between revisions

Line 2:
 
;Task:
Calculate the value of &nbsp; <big>''e''</big>.
 
 
(<big>''e''</big> &nbsp; is also known as &nbsp; ''Euler's number'' &nbsp; and &nbsp; ''Napier's constant''.)
 
 
Line 3,055:
{{out}}
<pre>e=2.718281828459046</pre>
 
=={{header|Python}}==
===Imperative===
Line 3,083 ⟶ 3,084:
;Using integer arithmetic only
Easily generate thousands of digits:
<syntaxhighlight lang="python">e = onerfct = 10 ** 1000
fact = n = 1
while fact < onerfct:
e += one // fact
n += 1
facte *+= nrfct
rfct //= n
print(f"{e}\n...in {n} steps")</syntaxhighlight>
{{out}}
Line 4,015 ⟶ 4,016:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh"># POSIX requires "signed long" for shell arithmetic, so assume to have at
# at least 31 bits available, andwhich dois 2:29sufficient bitsto store (e - fixed1) point* arithmetic10^9
 
one=1000000000
prec=29 one=$((1 << prec))
 
e=$one fact=10 n=0 rfct=$one
while [ $fact((rfct /= (n += 1))) -ltne $one0 ]
do
e=$((one / (fact *= (ne += 1)) + erfct))
done
 
echo "$((e / one + 1)).$((e % one))"</syntaxhighlight>
# 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.71828182718281823</pre>
 
=={{header|VBScript}}==
559

edits