Start punching in one interview question every day today

Knowledge about

System + Comprehensive

The universal Number

Inside JavaScript, all numbers are stored as 64-bit floating-point numbers, even integers. So, 1 is the same thing as 1.0, it’s the same number.

Do an experiment

Typeof 1 // number Typeof 1.0 // number 1 === 1.0Copy the code

The range of representations of the Number type

The structure of floating point numbers

According to the international standard IEEE 754, 64 binary bits of JavaScript floating point numbers.

  • Part 1 (blue) : Used to store sign bits, bit 1: sign bits,0For positive numbers,1According to a negative number
  • Part 2 (green) : Used to store exponents, bits 2 through 12: Exponents
  • Part 3 (red) : Store decimal digits digits 13 to 64: Store decimal parts (significant digits)

The fraction determines the range of safe representations of integers

That is

(-1)^s * f * 2^e

  • Sign part minus 1 or 1
  • The range of f is 1<=f<2, denoted by 52 bits
  • The exponent can be positive or negative. The length of the exponent bit is 11 bits, so the number that can be represented ranges from 0 to 2047

So it’s obvious that the maximum bit should be determined by f, which means that all 52 bits are used to represent the integer part.

Why do 52 places represent 53 decimal places

Because the decimal part only needs to represent the mantissa, and the integer part can definitely be equal to one

Fifty-two bits is too many to understand. Let’s say we have three bits

0.10 (binary) can be represented as 1.00 * 2^-1

0.01 (binary) can be represented as 1.00 * 2^-2

In this case, since the integer part must be equal to 1, we can omit the integer part.

So we can represent 3 digits and we can represent 4 digits when we’re doing decimals

Why is it not the exponential part

0.1123 * 2 ^ 1024

Of course, one of the questions is why it’s not the exponential part. Is the range of this number larger than the range of data we’re talking about.

This is not the case, because the practical index representation does not represent continuous numbers. So this plan is not desirable.

The range of representations of integers

Math.pow(2,53) -1 // maximum number. MAX_SAFE_INTEGER // constant indicates - (math.pow (2,53) -1) // maximum number. MIN_SAFE_INTEGER // constant indicatesCopy the code

The interview guide

Refining + system + self-consistency

  • Basic concept: Inside JavaScript, all numbers are stored as 64-bit floating point numbers.
  • Integers indicate ranges that are not parsed directly

review

  • This is a basic concept problem, but it’s easy to see how to represent floating-point numbers in binary, and the logic is complicated. It’s easy to see the basic skills and rational thinking skills.

    • Elementary: Know the Number
    • Intermediate: State the presentation scope derivation process
    • Advanced: The derivation process is reasonably analyzed and can be compared with other languages.