-1

Why is Number.MAX_VALUE+10 = 1.7976931348623157e+308 (the same like Number.MAX_VALUE) but Number.MAX_VALUE*2 = Infinity?

Jojo.Lechelt
  • 1,259
  • 12
  • 15
  • 1
    Downvoters care to comment? – caiocpricci2 Mar 10 '17 at 22:19
  • thx for downvoting but an answer would be more helpful. If I exceed the maximum value for a number I would expect to get Infinity as an answer. Why does it not appear when I add a low number like 10? – Jojo.Lechelt Mar 10 '17 at 22:19
  • I didn't understand the downvotes also. The question is very relevant. – Nelson Teixeira Mar 10 '17 at 22:33
  • On Stack Overflow, we want questions and answers about practical software programming problems. This does not seem to be practical. Also, it's been asked [before](http://stackoverflow.com/q/10837670/215552), which I was able to find as it was the third result when searching for Number.MAX_VALUE... – Heretic Monkey Mar 10 '17 at 23:22
  • @MikeMcCaughan well... its seems to me a practical software programming problem. The OP summed numbers and it didn't get the expected result. Practical enough. And about the question you pointed out, it may have a similar answer, but their both different in my view. – Nelson Teixeira Mar 10 '17 at 23:44
  • @NelsonTeixeira I'm struggling to see why, in a normal, everyday program, you would be adding 10 to Number.MAX_VALUE, or multiplying it by 2 for that matter. The OP accepted the duplicate, that's why it shows "Community" as the other closer. You're welcome to post a Meta question about the closure, or ask on the [SOCVR chat room](http://chat.stackoverflow.com/rooms/41570/so-close-vote-reviewers) for help in reopening if you feel strongly about the closure. – Heretic Monkey Mar 10 '17 at 23:58

1 Answers1

3

JavaScript uses a floating point representation which has 52 bits available for the fraction, while 11 bits are reserved for the exponent.

The precision of a number is dependent on the exponent. For 1e308 this precision is about 1e292. So anything smaller added to Number.MAX_VALUE will not change it. If however you do add something to it that is significant enough to change a bit in the fraction (like 1e292), then the number will change. As there is no larger number than Number.MAX_VALUE in this representation, except Infinity, the latter is the value you get.

trincot
  • 317,000
  • 35
  • 244
  • 286