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

130
Visualizações
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 Respostas
Responde à pergunta

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 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