Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
Why do I get -0 when dividing any number by -Infinity in JavaScript?

I was writing some JavaScript tests in Jest just to practice. I wrote the following function:

const divide = (a, b) => {
  if (isNaN(a) || isNaN(b)) {
    throw new Error("This function expect 2 numbers");
  } 
  return a / b;
};

Then I wrote the following test:

describe("Test divide operator", () => {
  it("Check the divide function", () => {
    expect(divide(10, 0)).toBe(Infinity);
    expect(divide(0, 0)).toBe(NaN);
    expect(divide(Infinity, Infinity)).toBe(NaN);
    expect(divide(10, Infinity)).toBe(0);
    expect(divide(10, -Infinity)).toBe(0);  // THIS TEST FAILS
  });
});

I received the following error:

    expect(received).toBe(expected) // Object.is equality

    Expected: 0
    Received: -0

Is it a JavaScript bug? Anyone knows why 0 can be negative in JavaScript? In my opinion, it is a little awkward to have to write "-0" just to pass the test.

Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is not a bug it is a "feature" from IEEE-754 about floating point arithmetic. -0 is a signed 0 and javascript implements it correctly.

It's extremely rare to actually encounter and controversial as well. Other languages handle it differently.

Also -0 === 0 AND -0 === +0 so it normaly does not matter.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I am sure it is not a bug, you can read more on -0 here Signed zero Wikiepdia

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda