Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:44:27+00:00 2026-06-01T11:44:27+00:00

Been looking through other answers and I still don’t understand the modulo for negative

  • 0

Been looking through other answers and I still don’t understand the modulo for negative numbers in python

For example the answer by df

x == (x/y)*y + (x%y)

so it makes sense that (-2)%5 = -2 – (-2/5)*5 = 3

Doesn’t this (-2 – (-2/5)*5) =0 or am I just crazy?
Modulus operation with negatives values – weird thing?

Same with this
negative numbers modulo in python
Where did he get -2 from?

Lastly if the sign is dependent on the dividend why don’t negative dividends have the same output as their positive counterparts?

For instance the output of

print([8%5,-8%5,4%5,-4%5])

is

[3, 2, 4, 1]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:44:28+00:00Added an answer on June 1, 2026 at 11:44 am

    In Python, modulo is calculated according to two rules:

    • (a // b) * b + (a % b) == a, and
    • a % b has the same sign as b.

    Combine this with the fact that integer division rounds down (towards −∞), and the resulting behavior is explained.

    If you do -8 // 5, you get -1.6 rounded down, which is -2. Multiply that by 5 and you get -10; 2 is the number that you’d have to add to that to get -8. Therefore, -8 % 5 is 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking through other questions/answers, but couldn't find anything that was helping. I
I've been looking through the Python Cookbook (2nd Edition) to learn how to process
I've been looking through the MSDN trying to understand the crytoapi. Below are some
I have been looking through the other questions on the site and have found
I have a pretty specific request. I have been looking through some other posts,
I've been looking through the answers about running php script in a background and
I've been looking through Stackoverflow for answers and there seems to be more than
I have been looking through code for the last 3 days, and the original
I have been looking through some code on an open source project recently and
I've been looking through various websites and came across multiple ways to make buttons.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.