Calculating the value of e

From Rosetta Code
Task
Calculating the value of e
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Calculate the value of   e.


(e   is also known as Euler's number   and   Napier's constant.)


See details: Calculating the value of e

ALGOL 68[edit]

Translation of: Kotlin
BEGIN
# calculate an approximation to e #
LONG REAL epsilon = 1.0e-15;
LONG INT fact := 1;
LONG REAL e := 2;
LONG INT n := 2;
WHILE
LONG REAL e0 = e;
fact *:= n;
n +:= 1;
e +:= 1.0 / fact;
ABS ( e - e0 ) >= epsilon
DO SKIP OD;
print( ( "e = ", fixed( e, -17, 15 ), newline ) )
END
Output:
e = 2.718281828459045

AppleScript[edit]

For the purposes of 32 bit floating point, the value seems to stabilise after summing c. 16 terms.

on run
 
sum(map(inverse, ¬
scanl(product, 1, enumFromToInt(1, 16))))
 
--> 2.718281828459
 
end run
 
-- inverse :: Float -> Float
on inverse(x)
1 / x
end inverse
 
-- product :: Float -> Float -> Float
on product(a, b)
a * b
end product
 
 
-- GENERIC FUNCTIONS ----------------------------------------
 
-- enumFromToInt :: Int -> Int -> [Int]
on enumFromToInt(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
return lst
else
return {}
end if
end enumFromToInt
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
-- iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
on iterateUntil(p, f, x)
script
property mp : mReturn(p)'s |λ|
property mf : mReturn(f)'s |λ|
property lst : {x}
on |λ|(v)
repeat until mp(v)
set v to mf(v)
set end of lst to v
end repeat
return lst
end |λ|
end script
|λ|(x) of result
end iterateUntil
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- scanl :: (b -> a -> b) -> b -> [a] -> [b]
on scanl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
set lst to {startValue}
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
set end of lst to v
end repeat
return lst
end tell
end scanl
 
-- sum :: [Num] -> Num
on sum(xs)
script add
on |λ|(a, b)
a + b
end |λ|
end script
 
foldl(add, 0, xs)
end sum
Output:
2.718281828459

AWK[edit]

 
# syntax: GAWK -f CALCULATING_THE_VALUE_OF_E.AWK
BEGIN {
epsilon = 1.0e-15
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 } }
 
Output:
e=2.718281828459046

C[edit]

Translation of: Kotlin
#include <stdio.h>
#include <math.h>
 
#define EPSILON 1.0e-15
 
int main() {
unsigned long long fact = 1;
double e = 2.0, e0;
int n = 2;
do {
e0 = e;
fact *= n++;
e += 1.0 / fact;
}
while (fabs(e - e0) >= EPSILON);
printf("e = %.15f\n", e);
return 0;
}
Output:
e = 2.718281828459046


C++[edit]

Invalid: e is not calculated as required.

#include <iostream>
#include <iomanip>
#include <cmath>
 
int main()
{
std::cout << "e = " << std::setprecision(18)
<< std::exp(1.0) << std::endl;
 
return 0;
}
 
Output:
e = 2.71828182845904509

C#[edit]

using System;
 
namespace CalculateE {
class Program {
public const double EPSILON = 1.0e-15;
 
static void Main(string[] args) {
ulong fact = 1;
double e = 2.0;
double e0;
uint n = 2;
do {
e0 = e;
fact *= n++;
e += 1.0 / fact;
} while (Math.Abs(e - e0) >= EPSILON);
Console.WriteLine("e = {0:F15}", e);
}
}
}
Output:
e = 2.718281828459050

D[edit]

import std.math;
import std.stdio;
 
enum EPSILON = 1.0e-15;
 
void main() {
ulong fact = 1;
double e = 2.0;
double e0;
int n = 2;
do {
e0 = e;
fact *= n++;
e += 1.0 / fact;
} while (abs(e - e0) >= EPSILON);
writefln("e = %.15f", e);
}
Output:
e = 2.718281828459046

F#[edit]

 
// A function to generate the sequence 1/n!). Nigel Galloway: May 9th., 2018
let e = Seq.unfold(fun (n,g)->Some(n,(n/g,g+1N))) (1N,1N)
 

Which may be used:

 
printfn "%.14f" (float (e |> Seq.take 20 |> Seq.sum))
 
Output:
2.71828182845905

Factor[edit]

Works with: Factor version 0.98
USING: math math.factorials prettyprint sequences ;
IN: rosetta-code.calculate-e
 
CONSTANT: terms 20
 
terms <iota> [ n! recip ] map-sum >float .
Output:
2.718281828459045

Fortran[edit]

 
Program eee
implicit none
integer, parameter :: QP = selected_real_kind(16)
real(QP), parameter :: one = 1.0
real(QP) :: ee
 
write(*,*) ' exp(1.) ', exp(1._QP)
 
ee = 1. +(one +(one +(one +(one +(one+ (one +(one +(one +(one +(one +(one &
+(one +(one +(one +(one +(one +(one +(one +(one +(one +(one) &
/21.)/20.)/19.)/18.)/17.)/16.)/15.)/14.)/13.)/12.)/11.)/10.)/9.) &
/8.)/7.)/6.)/5.)/4.)/3.)/2.)
 
write(*,*) ' polynomial ', ee
 
end Program eee
Output:
     exp(1.)    2.71828182845904523543
  polynomial    2.71828182845904523543

FreeBASIC[edit]

Normal basic[edit]

' version 02-07-2018
' compile with: fbc -s console
 
Dim As Double e , e1
Dim As ULongInt n = 1, n1 = 1
 
e = 1 / 1
 
While e <> e1
e1 = e
e += 1 / n
n1 += 1
n *= n1
Wend
 
Print "The value of e ="; e
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End
Output:
The value of e = 2.718281828459046

GMP version[edit]

Library: GMP
' version 02-07-2018
' compile with: fbc -s console
 
#Include "gmp.bi"
 
Sub value_of_e(e As Mpf_ptr)
 
Dim As ULong n = 1
Dim As Mpf_ptr e1, temp
e1 = Allocate(Len(__mpf_struct)) : Mpf_init(e1)
temp = Allocate(Len(__mpf_struct)) : Mpf_init(temp)
 
Dim As Mpz_ptr fac
fac = Allocate(Len(__mpz_struct)) : Mpz_init_set_ui(fac, 1)
 
Mpf_set_ui(e, 1) ' 1 / 0! = 1 / 1
 
While Mpf_cmp(e1, e) <> 0
Mpf_set(e1, e)
Mpf_set_z(temp, fac)
n+= 1
Mpz_mul_ui(fac, fac, n)
Mpf_ui_div(temp, 1, temp)
Mpf_add(e, e, temp)
Wend
 
End Sub
 
' ------=< MAIN >=------
 
Dim As UInteger prec = 50 ' precision = 50 digits
Dim As ZString Ptr outtext = Callocate (prec + 10)
Mpf_set_default_prec(prec * 3.5)
Dim As Mpf_ptr e
e = Allocate(Len(__mpf_struct)) : Mpf_init(e)
value_of_e(e)
 
Gmp_sprintf(outtext,"%.*Ff", prec, e)
 
Print "The value of e = "; *outtext
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End
Output:
The value of e = 2.71828182845904523536028747135266249775724709369996

Go[edit]

Translation of: Kotlin
package main
 
import (
"fmt"
"math"
)
 
const epsilon = 1.0e-15
 
func main() {
fact := uint64(1)
e := 2.0
n := uint64(2)
for {
e0 := e
fact *= n
n++
e += 1.0 / float64(fact)
if math.Abs(e - e0) < epsilon {
break
}
}
fmt.Printf("e = %.15f\n", e)
}
Output:
e = 2.718281828459046

Haskell[edit]

For the purposes of 64 bit floating point precision, the value seems to stabilise after summing c. 17-20 terms.

eApprox :: Double
eApprox = sum $ (1 /) <$> scanl (*) 1 [1 .. 20]
 
main :: IO ()
main = print eApprox
Output:
2.7182818284590455

Or equivalently, in a single fold:

import Data.List
 
eApprox2 :: Double
eApprox2 =
fst $
foldl' --' strict variant of foldl
(\(e, fl) x ->
let flx = fl * x
in (e + (1 / flx), flx))
(1, 1)
[1 .. 20]
 
main :: IO ()
main = print eApprox2
Output:
2.7182818284590455

Java[edit]

Translation of: Kotlin
public class CalculateE {
public static final double EPSILON = 1.0e-15;
 
public static void main(String[] args) {
long fact = 1;
double e = 2.0;
int n = 2;
double e0;
do {
e0 = e;
fact *= n++;
e += 1.0 / fact;
} while (Math.abs(e - e0) >= EPSILON);
System.out.printf("e = %.15f\n", e);
}
}
Output:
e = 2.718281828459046

JavaScript[edit]

(() => {
'use strict';
 
const e = () =>
sum(map(x => 1 / x,
scanl(
(a, x) => a * x,
1,
enumFromToInt(1, 20)
)
));
 
// GENERIC FUNCTIONS ----------------------------------
 
// enumFromToInt :: Int -> Int -> [Int]
const enumFromToInt = (m, n) =>
n >= m ? (
iterateUntil(x => x >= n, x => 1 + x, m)
) : [];
 
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
const iterateUntil = (p, f, x) => {
let vs = [x],
h = x;
while (!p(h))(h = f(h), vs.push(h));
return vs;
};
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// scanl :: (b -> a -> b) -> b -> [a] -> [b]
const scanl = (f, startValue, xs) =>
xs.reduce((a, x) => {
const v = f(a.acc, x);
return {
acc: v,
scan: a.scan.concat(v)
};
}, {
acc: startValue,
scan: [startValue]
})
.scan;
 
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
 
// MAIN -----------------------------------------------
return e();
})();
2.7182818284590455

Julia[edit]

Works with: Julia version 0.6

Module:

module NeperConstant
 
export NeperConst
 
struct NeperConst{T}
val::T
end
 
Base.show(io::IO, nc::NeperConst{T}) where T = print(io, "ℯ (", T, ") = ", nc.val)
 
function NeperConst{T}() where T
local e::T = 2.0
local e2::T = 1.0
local den::(T ≡ BigFloat ? BigInt : Int128) = 1
local n::typeof(den) = 2
while e ≠ e2
e2 = e
den *= n
n += one(n)
e += 1.0 / den
end
return NeperConst{T}(e)
end
 
end # module NeperConstant

Main:

for F in (Float16, Float32, Float64, BigFloat)
println(NeperConst{F}())
end
Output:
(Float16) 2.717
(Float32) 2.718282
(Float64) 2.7182818284590455
(BigFloat) 2.718281828459045235360287471352662497757247093699959574966967627724076630353416

Kotlin[edit]

// Version 1.2.40
 
import kotlin.math.abs
 
const val EPSILON = 1.0e-15
 
fun main(args: Array<String>) {
var fact = 1L
var e = 2.0
var n = 2
do {
val e0 = e
fact *= n++
e += 1.0 / fact
}
while (abs(e - e0) >= EPSILON)
println("e = %.15f".format(e))
}
Output:
e = 2.718281828459046

Lua[edit]

EPSILON = 1.0e-15;
 
fact = 1
e = 2.0
e0 = 0.0
n = 2
 
repeat
e0 = e
fact = fact * n
n = n + 1
e = e + 1.0 / fact
until (math.abs(e - e0) < EPSILON)
 
io.write(string.format("e = %.15f\n", e))
Output:
e = 2.718281828459046

Modula-2[edit]

MODULE CalculateE;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
 
CONST EPSILON = 1.0E-15;
 
PROCEDURE abs(n : REAL) : REAL;
BEGIN
IF n < 0.0 THEN
RETURN -n
END;
RETURN n
END abs;
 
VAR
buf : ARRAY[0..31] OF CHAR;
fact,n : LONGCARD;
e,e0 : LONGREAL;
BEGIN
fact := 1;
e := 2.0;
n := 2;
 
REPEAT
e0 := e;
fact := fact * n;
INC(n);
e := e + 1.0 / LFLOAT(fact)
UNTIL abs(e - e0) < EPSILON;
 
WriteString("e = ");
RealToStr(e, buf);
WriteString(buf);
WriteLn;
 
ReadChar
END CalculateE.

Perl 6[edit]

Works with: Rakudo version 2018.03
# If you need high precision: Sum of a Taylor series method.
# Adjust the terms parameter to suit. Theoretically the
# terms could be ∞. Practically, calculating an infinite
# series takes an awfully long time so limit to 500.
 
sub postfix:<!> (Int $n) { (constant f = 1, |[\*] 1..*)[$n] }
sub 𝑒 (Int $terms) { sum map { FatRat.new(1,.!) }, ^$terms }
 
say 𝑒(500).comb(80).join: "\n";
 
say '';
 
# Or, if you don't need high precision, it's a built-in.
say e;
Output:
2.718281828459045235360287471352662497757247093699959574966967627724076630353547
59457138217852516642742746639193200305992181741359662904357290033429526059563073
81323286279434907632338298807531952510190115738341879307021540891499348841675092
44761460668082264800168477411853742345442437107539077744992069551702761838606261
33138458300075204493382656029760673711320070932870912744374704723069697720931014
16928368190255151086574637721112523897844250569536967707854499699679468644549059
87931636889230098793127736178215424999229576351482208269895193668033182528869398
49646510582093923982948879332036250944311730123819706841614039701983767932068328
23764648042953118023287825098194558153017567173613320698112509961818815930416903
51598888519345807273866738589422879228499892086805825749279610484198444363463244
96848756023362482704197862320900216099023530436994184914631409343173814364054625
31520961836908887070167683964243781405927145635490613031072085103837505101157477
04171898610687396965521267154688957035035402123407849819334321068170121005627880
23519303322474501585390473041995777709350366041699732972508868769664035557071622
684471625608

2.71828182845905

Python[edit]

import math
#Implementation of Brother's formula
e0 = 0
e = 2
n = 0
fact = 1
while(e-e0 > 1e-15):
e0 = e
n += 1
fact *= 2*n*(2*n+1)
e += (2.*n+2)/fact
 
print "Computed e = "+str(e)
print "Real e = "+str(math.e)
print "Error = "+str(math.e-e)
print "Number of iterations = "+str(n)
Output:
Computed e = 2.71828182846
Real e = 2.71828182846
Error = 4.4408920985e-16
Number of iterations = 9

Racket[edit]

#lang racket
(require math/number-theory)
 
(define (calculate-e (terms 20))
(apply + (map (compose / factorial) (range terms))))
 
(module+ main
(let ((e (calculate-e)))
(displayln e)
(displayln (real->decimal-string e 20))
(displayln (real->decimal-string (- (exp 1) e) 20))))
Output:
82666416490601/30411275102208
2.71828182845904523493
0.00000000000000000000

REXX[edit]

This REXX version uses the following formula to calculate Napier's constant   e:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
║                                                                                       ║
║           1         1         1         1         1         1         1               ║
║   e  =   ───   +   ───   +   ───   +   ───   +   ───   +   ───   +   ───   +    ∙∙∙   ║
║           0!        1!        2!        3!        4!        5!        6!              ║
║                                                                                       ║
╚═══════════════════════════════════════════════════════════════════════════════════════╝

If the argument is negative, a running number of decimal digits of   e   is shown.

/*REXX pgm calculates  e  to any number of dec. digits.  If neg, running value is shown.*/
parse arg digs . /*get optional number of decimal digits*/
if digs=='' | digs=="," then digs=101 /*Not specified? Then use the default.*/
numeric digits abs(digs) /*use the absolute value of digs. */
tell= (digs<0) /* <0? Then show on-going calculations*/
w=length( abs(digs) ) /*W: used for aligning the output. */
@nap= "digits found for e (Napier's constant)" /*literal used for a SAY instruction.*/
e=1 /*define the value of e's 1st term. */
r=1 /* " " " " " " divisor*/
do #=1 until e==old /*start calculations at the second term*/
old=e /*save current value of e for COMPARE*/
r=r / # /*calculate the divisor for this term. */
e=e + r /*add quotient to running e value. */
if tell then do /*if digits is negative, display info. */
$=compare(e, old) /*determine number of e digs computed*/
if #>3 & $>0 then say "with" right(#+1,w) 'terms,' right($-1,w) @nap
end /* ↑ */
end /*#*/ /*-1 is for the decimal point──┘ */
 
say '(with' abs(digs) "decimal digits) the value of e is:"
say e /*stick a fork in it, we're all done. */

Programming note:   the factorial of the   do   loop index is calculated by   division,   not by the usual   multiplication   (for optimization).


output   when using the default input:
(with 101 decimal digits)   the value of   e   is:
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
output   when using the input of:   -101


(Shown at three-quarter size.)

with   5 terms,   2 digits found for  e  (Napier's constant)
with   6 terms,   3 digits found for  e  (Napier's constant)
with   7 terms,   4 digits found for  e  (Napier's constant)
with   8 terms,   5 digits found for  e  (Napier's constant)
with   9 terms,   6 digits found for  e  (Napier's constant)
with  10 terms,   6 digits found for  e  (Napier's constant)
with  11 terms,   8 digits found for  e  (Napier's constant)
with  12 terms,   9 digits found for  e  (Napier's constant)
with  13 terms,  10 digits found for  e  (Napier's constant)
with  14 terms,  11 digits found for  e  (Napier's constant)
with  15 terms,  12 digits found for  e  (Napier's constant)
with  16 terms,  14 digits found for  e  (Napier's constant)
with  17 terms,  13 digits found for  e  (Napier's constant)
with  18 terms,  16 digits found for  e  (Napier's constant)
with  19 terms,  17 digits found for  e  (Napier's constant)
with  20 terms,  18 digits found for  e  (Napier's constant)
with  21 terms,  19 digits found for  e  (Napier's constant)
with  22 terms,  21 digits found for  e  (Napier's constant)
with  23 terms,  21 digits found for  e  (Napier's constant)
with  24 terms,  24 digits found for  e  (Napier's constant)
with  25 terms,  25 digits found for  e  (Napier's constant)
with  26 terms,  27 digits found for  e  (Napier's constant)
with  27 terms,  27 digits found for  e  (Napier's constant)
with  28 terms,  29 digits found for  e  (Napier's constant)
with  29 terms,  30 digits found for  e  (Napier's constant)
with  30 terms,  32 digits found for  e  (Napier's constant)
with  31 terms,  33 digits found for  e  (Napier's constant)
with  32 terms,  35 digits found for  e  (Napier's constant)
with  33 terms,  37 digits found for  e  (Napier's constant)
with  34 terms,  38 digits found for  e  (Napier's constant)
with  35 terms,  40 digits found for  e  (Napier's constant)
with  36 terms,  41 digits found for  e  (Napier's constant)
with  37 terms,  43 digits found for  e  (Napier's constant)
with  38 terms,  45 digits found for  e  (Napier's constant)
with  39 terms,  46 digits found for  e  (Napier's constant)
with  40 terms,  48 digits found for  e  (Napier's constant)
with  41 terms,  49 digits found for  e  (Napier's constant)
with  42 terms,  51 digits found for  e  (Napier's constant)
with  43 terms,  52 digits found for  e  (Napier's constant)
with  44 terms,  54 digits found for  e  (Napier's constant)
with  45 terms,  56 digits found for  e  (Napier's constant)
with  46 terms,  57 digits found for  e  (Napier's constant)
with  47 terms,  59 digits found for  e  (Napier's constant)
with  48 terms,  61 digits found for  e  (Napier's constant)
with  49 terms,  62 digits found for  e  (Napier's constant)
with  50 terms,  64 digits found for  e  (Napier's constant)
with  51 terms,  65 digits found for  e  (Napier's constant)
with  52 terms,  67 digits found for  e  (Napier's constant)
with  53 terms,  69 digits found for  e  (Napier's constant)
with  54 terms,  71 digits found for  e  (Napier's constant)
with  55 terms,  72 digits found for  e  (Napier's constant)
with  56 terms,  74 digits found for  e  (Napier's constant)
with  57 terms,  76 digits found for  e  (Napier's constant)
with  58 terms,  78 digits found for  e  (Napier's constant)
with  59 terms,  80 digits found for  e  (Napier's constant)
with  60 terms,  81 digits found for  e  (Napier's constant)
with  61 terms,  83 digits found for  e  (Napier's constant)
with  62 terms,  84 digits found for  e  (Napier's constant)
with  63 terms,  87 digits found for  e  (Napier's constant)
with  64 terms,  88 digits found for  e  (Napier's constant)
with  65 terms,  91 digits found for  e  (Napier's constant)
with  66 terms,  92 digits found for  e  (Napier's constant)
with  67 terms,  94 digits found for  e  (Napier's constant)
with  68 terms,  96 digits found for  e  (Napier's constant)
with  69 terms,  98 digits found for  e  (Napier's constant)
with  70 terms, 100 digits found for  e  (Napier's constant)
with  71 terms, 101 digits found for  e  (Napier's constant)
(with 101 decimal digits)   the value of   e   is:
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274

Ring[edit]

 
# Project : Calculating the value of e
# Date  : 2018/04/21
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email  : <[email protected]>
 
decimals(14)
 
for n = 1 to 100000
e = pow((1 + 1/n),n)
next
see "Calculating the value of e with method #1:" + nl
see "e = " + e + nl
 
e = 0
for n = 0 to 12
e = e + (1 / factorial(n))
next
see "Calculating the value of e with method #2:" + nl
see "e = " + e + nl
 
func factorial(n)
if n = 0 or n = 1
return 1
else
return n * factorial(n-1)
ok
 

Output:

Calculating the value of e with method #1:
e = 2.71826823719230
Calculating the value of e with method #2:
e = 2.71828182828617

Scala[edit]

Output:
Best seen running in your browser either by ScalaFiddle (ES aka JavaScript, non JVM) or Scastie (remote JVM).
import scala.annotation.tailrec
 
object CalculateE extends App {
private val ε = 1.0e-15
 
@tailrec
def iter(fact: Long, ℯ: Double, n: Int, e0: Double): Double = {
val newFact = fact * n
val newE = ℯ + 1.0 / newFact
if (math.abs(newE - ℯ) < ε)
else iter(newFact, newE, n + 1, ℯ)
}
 
println(f"ℯ = ${iter(1L, 2.0, 2, 0)}%.15f")
}

Sidef[edit]

func calculate_e(prec=50) {
sum(^prec, {|n| 1/n! })
}
 
say calculate_e()
say calculate_e(100).as_dec(100)
Output:
2.7182818284590452353602874713526624977572470937
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427

zkl[edit]

Translation of: C
const EPSILON=1.0e-15;
fact,e,n := 1, 2.0, 2;
do{
e0:=e;
fact*=n; n+=1;
e+=1.0/fact;
}while((e - e0).abs() >= EPSILON);
println("e = %.15f".fmt(e));
Output:
e = 2.718281828459046