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 5850011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:04:53+00:00 2026-05-22T13:04:53+00:00

Non-positive number division is quite different in c++ and python programming langugages: //c++: 11

  • 0

Non-positive number division is quite different in c++ and python programming langugages:

//c++:
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2

So, as you can see, c++ is minimizing quotient.
However, python behaves like that:

#python
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -4
(-11) % 3 = 1
11 / (-3) = -4
11 % (-3) = -1
(-11) / (-3) = 3
(-11) % (-3) = -2

I can’t code my own division function behaving like c++, because I’ll use it for checking c++ calculator programs, and python does not support infix operators. Can I make python behaving like c++ while dividing integers in a simple way? For example, setting some flag or something like that?

  • 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-22T13:04:54+00:00Added an answer on May 22, 2026 at 1:04 pm

    As Thomas K said, use math.fmod for modulo, or if you really want you can define it yourself:

    def cmod(x, y):
        return abs(x) % abs(y) * (1 if x > 0 else -1)
    

    And this function should emulate C-style division:

    def cdiv(x, y):
        return abs(x) / abs(y) * cmp(x, 0) * cmp(y, 0)
    

    You said that you must use the / and % operators. This is not possible, since you can’t override the operator for built-ins. You can however define your own integer type and operator overload the __div__ and __mod__ operators.

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

Sidebar

Related Questions

What's the best algorithm to find the smallest non zero positive value from a
How can you strip non-ASCII characters from a string? (in C#)
The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and
A non-believer of C# was asking me what the purpose to extension methods was.
Speaking as a non-C# savvy programmer, I'm curious as to the evaluation semantics of
I have a non-.Net executable file that is included in my .net assembly as
Should be available to non-U.S. companies, easy to setup, reliable, cheap, customizable, etc. What
I'm calling a non-.NET dll from my project using P/Invoke, meaning that the .dll
Suppose I have a non-recurring event that needs to be raised X seconds from
I'm developing non-interactive cpu-bound application which does only computations, almost no IO. Currently it

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.