Calculating the value of e: Difference between revisions

Line 2,626:
" " input</syntaxhighlight>
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// Version 1.2.40
 
import kotlin.math.abs
Line 2,649:
e = 2.718281828459046
</pre>
 
This can also be done in a functional style. Empirically, 17 iterations are enough to get the maximum precision for 64-bit floating-point numbers. Also, for best results, we should sum smaller values before larger values; otherwise we can lose precision when adding a small value to a large value. The `asReversed()` call below is optional but highly recommended. The result of the calculation is identical to the standard library constant.
 
<syntaxhighlight lang="kotlin">fun main() {
val e = (1..17).runningFold(1L, Long::times)
.asReversed() // summing smaller values first improves accuracy
.sumOf { 1.0 / it }
println(e)
println(e == kotlin.math.E)
}</syntaxhighlight>
 
{{output}}
<pre>
2.718281828459045
true
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
44

edits