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

132
Vistas
Different result of modulo operation in JavaScript and Python

In javascript the modulo operation result is like this:

-9 % 19 = -9

while in python the same modulo operation yields this:

-9 % 19 = 10

now according to Finite Field python's answer is correct. can anyone please explain why JavaScript doesn't return result same as python?

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

0

It is a matter of conventions (see here) and, for Python it is related to the result of integer divisions. Python works on the basis that you can reverse the division and get back to the original numerator by multiplying the quotient by the denominator and adding the modulo:

So, if N divided by D gives a quotient of Q and remainder or R (N mod D), then Q x D + R = N

Applying the numbers (Python):

-9 / 19 --> Q = -1, R = 10  --> -1 x 19 + 10 = -9

this also means that 9 % -19 --> -10

9 / -19 --> Q = -1, R = -10 --> -1 x -19 + -10 = 9

Javascript uses a different approach for modulo of negative numbers (applying the sign of the numerator to the remainder of the division between absolute values) that does not reverse correctly when applying the mathematical definition to numerator and denominator that have different signs:

Math.floor(-9/19)) --> -1
-9 % 19            --> -9
                   --> -1 x 19 + -9 --> -28 

Math.floor(9/-19)) --> -1
9 % -19            --> 9
                   --> -1 x -19 + 9 --> 28 
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