Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
javascript: Why is Number.MAX_SAFE_INTEGER + 2.000000000000001 different than Number.MAX_SAFE_INTEGER + 2.0000000000000001?

It is said on Wikipedia that the representable numbers between 2⁵³ ~ 2⁵⁴ are all even integers.

If we let N = Number.MAX_SAFE_INTEGER (which is 2⁵³ - 1 = 9007199254740991), and calculate the following numbers: N+2, N+2.0000000000000001, N+2.000000000000001, I would expect that they are all the same, but I was wrong:

N + 2                  === 9007199254740992
N + 2.0000000000000001 === 9007199254740992
N + 2.000000000000001  === 9007199254740994

Does anyone know why?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first number, has a sub-decimal point component requiring 2^-56 exponent :

<<< '2.0000000000000001' gawk -v PREC=2000000 -nMbe '{
   
    printf("\n\t0x%.16A\n\n",($0)%1) }'

    0x 7.34ACA5F6226F0ADAp-56

the final 1 at the tail will be lost even during parsing, which means you're only asking it to handle

     [ 4^3^3/2 - 1 ] (ps:: this is my favorite 
                            way to express 2^53-1)
               + 2

which I'm guessing double-precision floating point logic defaults to not rounding it up. But conversely, the 2nd example you've provided has a sub-decimal component that is

     0x 4.80EBE7B9D58566C8 p-52

     i.e.

     0.00000000000000 10000000000
     0.000000000000000 2220446049 (~2^-52)

Combining both, what the floating point system sees is

     0x 2.000000000000 0 p+0 <~~~ 1st one
     0x 2.000000000000 5 p+0 <~~~ 2nd one 
                      /
                This 5 made all the difference

now the mantissa has something to use to round things up.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda