0

I tried to google it, but all key words references to funtions or solutions working with content of variable.

My question is simple. If variable represents a number,

var a = 1;

what is its max bit length? I mean, what highest number can it contain before buffer overflow happens? Is it int32? Is it int64? Is it a different length?

Thanks in advance

Zorak
  • 709
  • 7
  • 24
  • you mean http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – Hacketo Jan 29 '16 at 08:21
  • 1
    it won't buffer overflow in js... – dandavis Jan 29 '16 at 08:22
  • You mean "double precision floating point overflow" (or "double overflow"), or "integer overflow" (when a number stops being treated as an integer and starts being a double) - both of these pertain to JavaScript's numbers. You can't have a buffer overflow without a buffer - it's a completely different concept. (@dandavis: If you have an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), you can have buffer overflow in JS.) – Amadan Jan 29 '16 at 08:27
  • 1
    Numbers are not stored as integers in JS. Every number is a IEEE 754 floating point number. – Derek 朕會功夫 Jan 29 '16 at 08:42
  • @Derek朕會功夫: But even with floats, there is a threshold where `x + 1` is guaranteed to work the way one imagines integers should work - that's what I was referring to. (Also, the result of bit operations are true integers, and if you are specifically talking about storage, `Uint32Array` stores true integers; I agree that is not really relevant here, but "Every number is a IEEE 754" is not 100% correct.) – Amadan Feb 01 '16 at 02:04

2 Answers2

4

As the spec says, numbers in JavaScript are IEEE-754 double-precision floating point:

  • They're 64 bits in size.
  • Their range is -1.7976931348623157e+308 through 1.7976931348623157e+308 (that latter is available via Number.MAX_VALUE), which is to say the positive and negative versions of (2 - 2^-52) × 2^1023, but they can't perfectly represent all of those values. Famously, 0.1 + 0.2 comes out as 0.30000000000000004; see Is floating-point math broken?
  • The max "safe" integer value (whole number value that won't be imprecise) is 9,007,199,254,740,991, which is available as Number.MAX_SAFE_INTEGER on ES2015-compliant JavaScript engines.
  • Similarly, MIN_SAFE_INTEGER is -9,007,199,254,740,991
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Numbers in Javascript are IEEE 754 floating point double-precision values which has a 53-bit mantissa. See the MDN:

Integer range for Number

The following example shows minimum and maximum integer values that can be represented as Number object (for details, refer to ECMAScript standard, chapter 8.5 The Number Type):

var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331