Math.E is 0.99 .... ^ max int

A friend showed me that (at least on the Google Chrome console) the following statement outputs true:

1/Math.pow(0.9999999999999999, Number.MAX_SAFE_INTEGER) === Math.E 

And indeed, 1/Math.pow(0.9999999999999999, Number.MAX_SAFE_INTEGER) is 2.718281828459045 .

This can't be a coincidence ?!

Can someone explain what is going on behind the scenes to make this work?

According to wolfram alpha , the correct value should be approximately 1/0.40628 , which is approximately equal to 2.4613566998129373 - very far from Math.E (I assume that wolframalpha is more accurate in its calculations than javascript, but I could be wrong).

Any explanation would be appreciated.

Bonus: What is the true approximate mathematical meaning of this expression, interesting? I found this:

 n = 0.0000000000000001 (1 - n)^MAX_INT = 1 + (MAX_INT choose 2) * n + (MAX_INT choose 3) * n^2 + ... + n^MAX_INT 

but I don’t know how to bring it closer.

I tested the above expression in wolfram alpha and got 2.46 .

+6
source share
2 answers

This is due to the initial characteristic e as :

e equation

Then, using a property that:

  • MAX_SAFE_INTEGER = 2 53 -1 and
  • 0.9999999999999999 rounds to 1 - 2 -53

then

1 / (1-2 -53 ) = 1 + 2 -53 / (1-2 -53 ) = 1 + 1 / (2 53 -1)

Hence,

1 / (1-2 -53 ) 2 53 -1 = [1 + 1 / (2 53 -1)] 2 53 -1

which is very close to e.

+4
source

pow(x, y) usually computed as exp(log(x) * y) , so let's get started there.

We have:

  • x = 0.9999999999999999 , which is rounded to x = 1 - eps (where eps == 2^-53 ).
  • y = 2^53 - 1 i.e. y = 1 / eps (approximately).

So, we actually compute exp(log(1 - eps) * 1/eps) .

The extension of the Taylor log(1 - k) series log(1 - k) is -k - k^2/2 - ... , but in our case all members of a higher order will be truncated.

So we have exp(-eps / eps) or exp(-1) , which is 1 / e .


<Sub> Demonstration:
 1 - 0.9999999999999999 // 1.1102230246251565e-16 Math.log(1 - 1.1102230246251565e-16) // -1.1102230246251565e-16 1 / Number.MAX_SAFE_INTEGER // 1.1102230246251568e-16 

sub>

+7
source

Source: https://habr.com/ru/post/979585/


All Articles