Calculating the value of e: Difference between revisions
→{{header|langur}}
(→dc: calculate reciprocal factorial directly, avoiding division by large numbers, ~100 x faster with GNU dc) |
Langurmonkey (talk | contribs) |
||
(22 intermediate revisions by 14 users not shown) | |||
Line 438:
{{out}}
<pre>The value of e = 2.71828182829</pre>
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> @%=&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
END</syntaxhighlight>
{{Out}}
<pre>e = 2.718281828459045226</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">10 CLS
20 N = 1 : N1 = 1
30 E1 = 0 : E = 1/1
40 WHILE E <> E1
50 E1 = E
60 E = E+1/N
70 N1 = N1+1
80 N = N*N1
90 WEND
100 PRINT "The value of E = " E
110 END</syntaxhighlight>
{{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}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
==={{header|MSX Basic}}===
The [[#Commodore_BASIC|Commodore BASIC]] solution works without any changes.
==={{header|QBasic}}===
Line 877 ⟶ 931:
loop e <> f
print "The value of e = ", e</syntaxhighlight>
{{out| Output}}<pre>The value of e = 2.72</pre>
=={{header|D}}==
Line 1,075 ⟶ 1,128:
<pre>2.71828182845905</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
numfmt 5 0
fact = 1
n = 2
Line 1,085 ⟶ 1,139:
e += 1 / fact
.
print e
</syntaxhighlight>
=={{header|EDSAC order code}}==
===Simple arithmetic===
Line 1,990 ⟶ 2,046:
</pre>
=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude math.uh
1.0e-15 sto EPSILON
one fact
2. sto en
2 sto nn
ciklus:
@en sto e0
#g @nn++ prd fact
1.0 @fact !(#d) / sum en
@en @e0 - abs @EPSILON < else §ciklus
."e = " @en printnl
end
{ „EPSILON” }
{ „fact” }
{ „en” }
{ „e0” }
{ „nn” }
</syntaxhighlight>
{{out}}
<pre>
e = +2.71828182845905
</pre>
=={{header|FutureBasic}}==
Line 2,018 ⟶ 2,100:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Calculating_the_value_of_e}}
'''Solution 1.''' In Fōrmulæ, the Euler's number can be directly calculated within an arbitrary given precision.
[[File:Fōrmulæ - Calculating the value of e 01.png]]
[[File:Fōrmulæ - Calculating the value of e 02.png]]
'''Solution 2.''' Using series
[[File:Fōrmulæ - Calculating the value of e 03.png]]
[[File:Fōrmulæ - Calculating the value of e 04.png]]
'''Solution 3.''' Using a program
[[File:Fōrmulæ - Calculating the value of e 05.png]]
[[File:Fōrmulæ - Calculating the value of e 06.png]]
=={{header|Go}}==
{{trans|Kotlin}}
Line 2,181 ⟶ 2,278:
computed e 2.718281828459046
keyword &e 2.718281828459045</pre>
=={{header|J}}==
Perhaps too brief:
Line 2,275 ⟶ 2,363:
=={{header|Java}}==
<p>
If you wanted to do this with the basic, bounded, primitive data-types, you could use the following implementation.
</p>
<syntaxhighlight lang="java">
double e(long limit) {
double e = 1;
for (long term = 1; term <= limit; term++)
e += 1d / factorial(term);
return e;
}
long factorial(long value) {
return value == 1 ? value : value * factorial(--value);
}
</syntaxhighlight>
Here is an execution with 65 terms.
<pre>
2.7182818284590455
</pre>
<p>
Alternately, this can be done using the <code>BigDecimal</code> and <code>BigInteger</code> classes.<br />
Each which offer the ability to compute values beyond the constraints of both <code>double</code> and <code>long</code>, respectively.
</p>
<p>
I used a <kbd>rounding-mode</kbd> of "half-up", which rounds at 0.5 to 1, and at -0.5 to -1.
</p>
<p>
You can set the scale with the <kbd>scale</kbd> argument, which will ultimately be the <code>int</code> maximum, which is 2,147,483,647.
</p>
<syntaxhighlight lang="java">
import static java.math.RoundingMode.HALF_UP;
import java.math.BigDecimal;
import java.math.BigInteger;
</syntaxhighlight>
<syntaxhighlight lang="java">
BigDecimal e(BigInteger limit, int scale) {
BigDecimal e = BigDecimal.ONE.setScale(scale, HALF_UP);
BigDecimal n;
BigInteger term = BigInteger.ONE;
while (term.compareTo(limit) <= 0) {
n = new BigDecimal(String.valueOf(factorial(term)));
e = e.add(BigDecimal.ONE.divide(n, scale, HALF_UP));
term = term.add(BigInteger.ONE);
}
return e;
}
BigInteger factorial(BigInteger value) {
if (value.compareTo(BigInteger.ONE) > 0) {
return value.multiply(factorial(value.subtract(BigInteger.ONE)));
} else {
return BigInteger.ONE;
}
}
</syntaxhighlight>
<p>
Here is a execution using 100 terms, and a scale of 1000.
</p>
<syntaxhighlight lang="java">
BigDecimal e = e(BigInteger.valueOf(100), 1000);
</syntaxhighlight>
<pre>
2.718281828459045235360287471352662497757247093699959574966967627724076630353547
59457138217852516642742746639193200305992181741359662904357290033429526059563073
80251882050351967424723324653614466387706813388353430034139974174573454428990064
20505625641288373144866257181218169558839675319515277938384101062576645231282166
05239098395155129969477630231658550707025356578811143735087981194929233692894922
18803532415232263419815664514069500471960037083157525314472746953376422185585970
49072791947918398790373277485203358571021151276932683987895240761723360600431495
39142130255178704446106587993897518897018555141736300685184133175671746042499671
27787770519325213151660446607713922514023318509164691235154486907393983388487303
71051660145947922319807187020285702323658476026591320639392924337656895542820166
62247170847987607703419588398315167187799994627689255589861192622972941592318112
80945595959314875046163012079992544503657235607075679104723168137049185311508243
129582873013636538450324418025276976013009
</pre>
<br />
Or, you could use the following implementation.
{{trans|Kotlin}}
<syntaxhighlight lang="java">public class CalculateE {
Line 2,294 ⟶ 2,460:
{{out}}
<pre>e = 2.718281828459046</pre>
=={{header|Javascript}}==
Summing over a scan
Line 2,388 ⟶ 2,555:
{{Out}}
<pre>2.7182818284590455</pre>
=={{header|Joy}}==
<syntaxhighlight lang="joy">1 dup 2 52 pow dup [/ +] dip pow.</syntaxhighlight>
{{out}}
<pre>2.71828</pre>
=={{header|jq}}==
<syntaxhighlight lang="text">1|exp #=> 2.718281828459045</syntaxhighlight>
Line 2,395 ⟶ 2,568:
| .[0];
e #=> 2.7182818284590455</syntaxhighlight>
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 2,476 ⟶ 2,650:
" " input</syntaxhighlight>
=={{header|Kotlin}}==
<syntaxhighlight lang="
import kotlin.math.abs
Line 2,499 ⟶ 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,537 ⟶ 2,728:
for .fact, .n = 1, 2 ; ; .n += 1 {
val .e0 = .e
.fact
.e += 1 / .fact
if abs(.e - .e0) < .epsilon: break
Line 2,553 ⟶ 2,744:
e = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746
</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">EPSILON = 1.0e-15;
Line 2,638 ⟶ 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}}==
{{works with|min|0.37.0}}
<syntaxhighlight lang="min">1 0
(dup 16 >) 'pop (succ over over / swap) '+ linrec
puts!</syntaxhighlight>
{{out}}
<pre>2.718281828459045</pre>
=={{header|МК-61/52}}==
<syntaxhighlight lang="mk-61">П0 П1 0 П2 1 П2 1 П3
Line 3,297 ⟶ 3,503:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|
<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,303 ⟶ 3,509:
# series takes an awfully long time so limit to 500.
constant 𝑒 = [\+]
.say for 𝑒[500].comb(80);
Line 4,014 ⟶ 4,220:
# least 31 bits available, which is sufficient to store (e - 1) * 10^9
declare -ir one=10**9
declare -i e n rfct=one
do e+=rfct
done
Line 4,124 ⟶ 4,329:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var fact = 1
var e = 2
Line 4,141 ⟶ 4,346:
e = 2.718281828459
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">real N, E, E0, F; \index, Euler numbers, factorial
Line 4,161 ⟶ 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");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
var n: u32 = 0;
var state: u2 = 0;
Line 4,195 ⟶ 4,402:
},
}
if
const ov3
if
const ov4 = @addWithOverflow(ov3[0], q0);
if (ov4[1] != 0) break;
const q2 = ov4[0];
try stdout.print("e ~= {d:>19} / {d:>19} = ", .{ p2, q2 });
try decPrint(stdout, p2, q2, 36);
try stdout.writeByte('\n');
p0 = p1;
Line 4,214 ⟶ 4,423:
}
fn
// print out integer part.
try ostream.print("{}.", .{num / den});
Line 4,221 ⟶ 4,430:
// multiply by 10 could potentially overflow a u64.
//
const m: u128 = @intCast(
var r = @as(u128, num) % m;
var dec: usize = 0; // decimal place we're in.
Line 4,228 ⟶ 4,437:
r = n % m;
dec += 1;
const ch = @
try ostream.writeByte(ch);
}
Line 4,277 ⟶ 4,486:
e ~= 5739439214861417731 / 2111421691000680031 = 2.718281828459045235360287471352662497
</pre>
=={{header|zkl}}==
{{trans|C}}
|