I have created an algorithm for the base of the natural logarithm with HTML and JS. And this is the code:
HTML: bonl.html
<html>
<head>
<title>bonl</title>
</head>
<body>
<script src="bonl.js"></script>
<input type="number" id="entered">
<input type="button" value="run" onclick="calculate()">
<div id="bonl">
</body>
</html>
and Javascript: bonl.js
function calculate() {
var x = document.getElementById('entered').value;
console.log(x);
var e = (1 + (1/x))**x;
console.log(e);
document.getElementById('bonl').innerHTML = e;
}
First, you assign a number value to <input type="number" id="entered">, and click the button named 'run'. After that, var x in bonl.js will be equal to the number assigned to 'entered'. Then, according to the definition of the base of the natural logarithm (e = lim x->inf (1+(1/x)**x)), the Javascript file will calculate the e. And the result will be displayed by <div id="bonl">.
I hope you noticed that as the value of the x gets larger, javascript file calculates the base of the natural logarithm more accurately.
However, I entered about 10 quadrillion in <input type="number" id="entered">, and I've got the result of 1 instead of 2.71828.., although I get 2.71828 when I enter 100 trillion in <input type="number" id="entered">.
Is my computer dumb to calculate e, or is there an error in my code, or is e = 1?
Yes, your computer is dumb. It can only operate floating point numbers below 2^53. When you go above that, it loses the precision and
1 + small number
becomes just1
:Can we do better than that? Yes, we can! Instead of computing
e
using floating point numbers, let's computesome_big_number * e
using Big Integers, which, unlike floats, have unlimited precision. Using BigInts we can compute as many terms of the power series as we want: