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

  • Home
  • SEARCH
  • 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 6867577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:19:37+00:00 2026-05-27T03:19:37+00:00

Python 2.7.1: print -34 % 4 # outputs 2 Java 1.5.0: System.out.println(-34 % 4);

  • 0

Python 2.7.1:

print -34 % 4 # outputs 2

Java 1.5.0:

System.out.println(-34 % 4); // outputs -2

Which is correct? Why the difference?

The wikipedia article on modulo says

When either a or n is negative, this naive definition breaks down and programming languages differ in how these values are defined.

Maybe this isn’t a proper SO question and will get deleted but I’d be interested to see the responses.

  • 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-05-27T03:19:38+00:00Added an answer on May 27, 2026 at 3:19 am

    It’s just a convention the language has chosen. There is no one choice that is more mathematically correct than another.

    Hints for the reasons for Python’s choice can be found in the source: http://hg.python.org/cpython/file/2.7/Objects/intobject.c#l567

    Python’s version is the one more commonly used in number theory. The Java version implements the same behavior as C. The Python way is convenient because the sign of the result is always the same sign as the divisor. This means that x % n always gives a value in 0 <= result < n when n is positive.

    I speculate that the reason a lower level language like C uses a different convention is that it is “closer to the metal” returning the same result as n signed division in machine language (see IDIV for example). If C were to have adopted the number theory convention, it would need to compile additional instructions to compare the sign of the result to the sign of the divisor and if they didn’t match then jump to additional code to add or substract the divisor from the result (IOW, C adopts a convention that corresponds to minimal effort).

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

Sidebar

Related Questions

I'm using Python's logging mechanism to print output to the screen. I could do
How do I force Python's print function to flush the buffered output to the
Is there any alternatives to the print statement for output in Python. Also, how
I tried running a python script: print Hello, World! And I get this error:
Python does not print traceback messages from exceptions raised in daemon threads. For example,
Consider these examples using print in Python: >>> for i in range(4): print('.') .
In python you can use a tuple in a formatted print statement and the
In python, if I say print 'h' I get the letter h and a
In Python, how can I print the current call stack from within a method
in the following python code: narg=len(sys.argv) print @length arg= , narg if narg ==

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.