javascript - Math.E equals 0.99.... ^ max int - Stack Overflow

A friend showed me that (at least, in the google chrome console) the following statement prints true:1

A friend showed me that (at least, in the google chrome console) the following statement prints 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?!

Could 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 2.4613566998129373 -- very far off from Math.E. (I am assuming that wolframalpha is more precise in its calculations than javascript, but I may be wrong).

Any explanation would be appreciated.

Bonus: What is the true approximate mathematical value of that expression, I wonder? 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 have no idea how to approximate that.

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

A friend showed me that (at least, in the google chrome console) the following statement prints 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?!

Could 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 2.4613566998129373 -- very far off from Math.E. (I am assuming that wolframalpha is more precise in its calculations than javascript, but I may be wrong).

Any explanation would be appreciated.

Bonus: What is the true approximate mathematical value of that expression, I wonder? 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 have no idea how to approximate that.

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

Share Improve this question edited Dec 14, 2014 at 18:18 soktinpk asked Dec 14, 2014 at 18:01 soktinpksoktinpk 3,8982 gold badges24 silver badges34 bronze badges 7
  • According to wolfram alpha it is the same. – Roger Far Commented Dec 14, 2014 at 18:05
  • @Rogier21 What do you mean it's the same? wolframalpha./input/… – soktinpk Commented Dec 14, 2014 at 18:09
  • euler's number is in WA 2.718 but of you check the link above it will give the same answer as Javascript. e == 0.99^maxint = true, while the decimal values are no equal. So both JS and WA do give the same answers. – Roger Far Commented Dec 14, 2014 at 18:13
  • 1 @Rogier21 I don't think that's valid since even wolframalpha./input/… that link returns true. Wolfram alpha has gone crazy! (e == e + 1?) – soktinpk Commented Dec 14, 2014 at 18:14
  • Confirmed Node.js gets the same if you define Number.MAX_SAFE_INTEGER. Numbers above 0.9999... are rounded to 1, so that's basically (1 - epsilon) where epsilon is the smallest unit the number can represent at that scale. This probably has something to do with how the power converges to e, but not sure how. – rich remer Commented Dec 14, 2014 at 18:15
 |  Show 2 more ments

2 Answers 2

Reset to default 7

pow(x, y) is typically puted as exp(log(x) * y), so let's start there.

We have:

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

So we're actually calculating exp(log(1 - eps) * 1/eps).

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

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


Demonstration:

1 - 0.9999999999999999                  //  1.1102230246251565e-16
Math.log(1 - 1.1102230246251565e-16)    // -1.1102230246251565e-16
1 / Number.MAX_SAFE_INTEGER             //  1.1102230246251568e-16

This arises from the original characterisation of e as:

Then using the property that:

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

then

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

Therefore,

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

which is very close to e.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744990511a4604874.html

相关推荐

  • javascript - Math.E equals 0.99.... ^ max int - Stack Overflow

    A friend showed me that (at least, in the google chrome console) the following statement prints true:1

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信