Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!