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

5
  • you mean stackoverflow.com/questions/307179/… Commented Jan 29, 2016 at 8:21
  • 1
    it won't buffer overflow in js... Commented Jan 29, 2016 at 8: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, you can have buffer overflow in JS.) Commented Jan 29, 2016 at 8:27
  • 1
    Numbers are not stored as integers in JS. Every number is a IEEE 754 floating point number. Commented Jan 29, 2016 at 8: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.) Commented Feb 1, 2016 at 2:04

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.