Calculating the value of e: Difference between revisions

(13 intermediate revisions by 9 users not shown)
Line 438:
{{out}}
<pre>The value of e = 2.71828182829</pre>
 
==={{header|IS-BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="is-basicbbcbasic">100 PROGRAM "e.bas" @%=&1302
EPSILON = 1.0E-15
Fact { = 1
E = 2.0
E0 = 0.0
N% = 2
WHILE ABS(E - E0) >= EPSILON
E0 = E
Fact *= N%
N% += 1
E += 1.0 / Fact
ENDWHILE
PRINT "e = ";E
160 PRINT "The value of e =";EEND</syntaxhighlight>
{{Out}}
<pre>e = 2.718281828459045226</pre>
 
==={{header|Chipmunk Basic}}===
Line 456 ⟶ 475:
{{out}}
<pre>The value of E = 2.718282</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "e.bas"
110 LET E1=0:LET E,N,N1=1
120 DO WHILE E<>E1
130 LET E1=E:LET E=E+1/N
140 LET N1=N1+1:LET N=N*N1
150 LOOP
160 PRINT "The value of e =";E</syntaxhighlight>
{{Out}}
<pre>The value of e = 2.71828183</pre>
 
==={{header|GW-BASIC}}===
Line 1,098 ⟶ 1,128:
<pre>2.71828182845905</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="text">numfmt 0 5
numfmt 5 0
fact = 1
n = 2
Line 1,108 ⟶ 1,139:
e += 1 / fact
.
print e</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
===Simple arithmetic===
Line 2,245 ⟶ 2,278:
computed e 2.718281828459046
keyword &e 2.718281828459045</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "e.bas"
110 LET E1=0:LET E,N,N1=1
120 DO WHILE E<>E1
130 LET E1=E:LET E=E+1/N
140 LET N1=N1+1:LET N=N*N1
150 LOOP
160 PRINT "The value of e =";E</syntaxhighlight>
{{Out}}
<pre>The value of e = 2.71828183</pre>
=={{header|J}}==
Perhaps too brief:
Line 2,626 ⟶ 2,650:
" " input</syntaxhighlight>
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// Version 1.2.40
 
import kotlin.math.abs
Line 2,649 ⟶ 2,673:
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">
Line 2,687 ⟶ 2,728:
for .fact, .n = 1, 2 ; ; .n += 1 {
val .e0 = .e
.fact x*= .n
.e += 1 / .fact
if abs(.e - .e0) < .epsilon: break
Line 2,703 ⟶ 2,744:
e = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">EPSILON = 1.0e-15;
Line 2,788 ⟶ 2,830:
{{output}}
<pre>𝕖</pre>
 
=={{header|Maxima}}==
Using the expansion of an associated continued fraction
<syntaxhighlight lang="maxima">
block(cfexpand([2,1,2,1,1,4,1,1,6,1,1,8,1,1,10,1,1,12,1,1,14]),float(%%[1,1]/%%[2,1]));
 
/* Comparing with built-in constant */
%e,numer;
</syntaxhighlight>
{{out}}
<pre>
2.718281828459045
 
2.718281828459045
</pre>
 
=={{header|min}}==
Line 3,446 ⟶ 3,503:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|20222023.0709}}
<syntaxhighlight lang="raku" line># If you need high precision: Sum of a Taylor series method.
# Adjust the terms parameter to suit. Theoretically the
Line 3,452 ⟶ 3,509:
# series takes an awfully long time so limit to 500.
 
constant 𝑒 = [\+] 1.FatRat X/flat 1, |[\*/] 1.FatRat..*;
 
.say for 𝑒[500].comb(80);
Line 4,163 ⟶ 4,220:
# least 31 bits available, which is sufficient to store (e - 1) * 10^9
 
declare -ir one=10**9
one=1000000000
declare -i e n rfct=one
 
e=0while n=0(( rfct /=$one ++n ))
do e+=rfct
while [ $((rfct /= (n += 1))) -ne 0 ]
do
e=$((e + rfct))
done
 
Line 4,273 ⟶ 4,329:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var epsilon = 1e-15
var fact = 1
var e = 2
Line 4,290 ⟶ 4,346:
e = 2.718281828459
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">real N, E, E0, F; \index, Euler numbers, factorial
Line 4,310 ⟶ 4,367:
</pre>
=={{header|Zig}}==
{{works with|Zig|0.11.0}}
This uses the continued fraction method to generate the maximum ratio that can be computed using 64 bit math. The final ratio is correct to 35 decimal places.
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math;
const stdout = std.io.getStdOut().writer();
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
var n: u32 = 0;
var state: u2 = 0;
Line 4,344 ⟶ 4,402:
},
}
varconst p2: u64ov1 = undefined@mulWithOverflow(a, p1);
varif q2: u64(ov1[1] != undefined0) break;
ifconst ov2 = (@mulWithOverflowaddWithOverflow(u64ov1[0], a, p1, &p2p0) or;
if @addWithOverflow(u64, p2,ov2[1] p0,!= &p20) orbreak;
const ov3 = @mulWithOverflow(u64, a, q1, &q2) or;
if @addWithOverflow(u64,ov3[1] q2,!= q0,0) &q2))break;
const ov4 = @addWithOverflow(ov3[0], q0);
{
if (ov4[1] != 0) break;
}const p2 = ov2[0];
const q2 = ov4[0];
try stdout.print("e ~= {d:>19} / {d:>19} = ", .{p2, q2});
 
try dec_print(stdout, p2, q2, 36);
try stdout.print("e ~= {d:>19} / {d:>19} = ", .{ p2, q2 });
try dec_printdecPrint(stdout, p2, q2, 36);
try stdout.writeByte('\n');
p0 = p1;
Line 4,363 ⟶ 4,423:
}
 
fn dec_printdecPrint(ostream: anytype, num: u64, den: u64, prec: usize) !void {
// print out integer part.
try ostream.print("{}.", .{num / den});
Line 4,370 ⟶ 4,430:
// multiply by 10 could potentially overflow a u64.
//
const m: u128 = @intCast(u128, den);
var r = @as(u128, num) % m;
var dec: usize = 0; // decimal place we're in.
Line 4,377 ⟶ 4,437:
r = n % m;
dec += 1;
const ch = @intCastas(u8, @intCast(n / m)) + '0';
try ostream.writeByte(ch);
}
Line 4,426 ⟶ 4,486:
e ~= 5739439214861417731 / 2111421691000680031 = 2.718281828459045235360287471352662497
</pre>
 
=={{header|zkl}}==
{{trans|C}}
885

edits