Python negative division and modulo operation

Source: Internet
Author: User
Tags modulus

1. Negative Division:

>>> Print 45/7
6
>>> PRINT-45/7
-7

>>> Print 45/-7
-7

>>> print-45/-7
6

For the first example: the first number contains the number of the second number 45 contains how many 7 its result is 6

For the second example: what is the distance between the first number and the second number-the distance between 45 and 7 is 52 52/7=7 The result is a sign of-7

Can be understood as-((45+7)/7)

2. Modulo operation

The two operations of modulus and redundancy cannot be confused, and no negative numbers are pressed to calculate the remainder in C/s + + java. It's not the same in Python.

>>> Print 45%7
3
>>> print-45%7
4
>>> Print 45%-7
-4

>>> print-45%-7
-3

We all know the formula for modulus or redundancy: c=a/b;r=a-c*b

The result of the Python modulo is to determine whether the result is positive or negative based on the second number.

C + + Java is determined by the first number of its results

The current environment is python2.7.6 do not know the low version and the high version of what is different have been verified please inform. ......

Python negative division and modulo operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.