Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail at divide a negative integer number for a positive integer number #74001

Closed
thomazs mannequin opened this issue Mar 15, 2017 · 6 comments
Closed

Fail at divide a negative integer number for a positive integer number #74001

thomazs mannequin opened this issue Mar 15, 2017 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@thomazs
Copy link
Mannequin

thomazs mannequin commented Mar 15, 2017

BPO 29815
Nosy @vadmium, @eryksun, @thomazs

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-03-15.10:39:48.842>
created_at = <Date 2017-03-15.07:56:40.440>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Fail at divide a negative integer number for a positive integer number'
updated_at = <Date 2017-03-15.10:39:48.839>
user = 'https://github.com/thomazs'

bugs.python.org fields:

activity = <Date 2017-03-15.10:39:48.839>
actor = 'eryksun'
assignee = 'none'
closed = True
closed_date = <Date 2017-03-15.10:39:48.842>
closer = 'eryksun'
components = ['Interpreter Core']
creation = <Date 2017-03-15.07:56:40.440>
creator = 'marcosthomazs'
dependencies = []
files = []
hgrepos = []
issue_num = 29815
keywords = []
message_count = 6.0
messages = ['289647', '289648', '289655', '289656', '289659', '289665']
nosy_count = 3.0
nosy_names = ['martin.panter', 'eryksun', 'marcosthomazs']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue29815'
versions = ['Python 2.7', 'Python 3.5']

@thomazs
Copy link
Mannequin Author

thomazs mannequin commented Mar 15, 2017

At divide a negative integer number for a positive integer number, the result is wrong. For example, in operation:

a, b, c = -7, 2, 7
d = divmod(a, b)
print a//b, a%b, c[0], c // b, c%b

The values printed are -4 1 3 1

@thomazs thomazs mannequin added type-crash A hard crash of the interpreter, possibly with a core dump interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 15, 2017
@vadmium
Copy link
Member

vadmium commented Mar 15, 2017

If you ignore the c[0] argument, the rest looks fine to me. See the documentation at <https://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations\> and <https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex\>.

The double-slash operator is called “floor division”. If you are expecting some other rounding, see <https://docs.python.org/2/faq/programming.html#why-does-22-10-return-3\>.

@vadmium vadmium closed this as completed Mar 15, 2017
@vadmium vadmium added invalid type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 15, 2017
@thomazs
Copy link
Mannequin Author

thomazs mannequin commented Mar 15, 2017

I'm sorry, but was a typing error. Try this operations:

a, b, c = 7, -7, 2
print "1:", a // c, a % c
print "2:", b // c, b % c

the result is:
1: 3 1
2: -4 1

The division is the same, except by the signal (variable b is negative, but both, variables "a" and "b" are integers).

@thomazs
Copy link
Mannequin Author

thomazs mannequin commented Mar 15, 2017

Try this operations, in interactive environment:

> 7 // 2
3
> -7 // 2
-4

@thomazs thomazs mannequin removed the invalid label Mar 15, 2017
@thomazs thomazs mannequin reopened this Mar 15, 2017
@thomazs
Copy link
Mannequin Author

thomazs mannequin commented Mar 15, 2017

Note that mathematic expression is wrong. -7 divided by 2 is -3, not -4

@thomazs thomazs mannequin added type-crash A hard crash of the interpreter, possibly with a core dump and removed type-bug An unexpected behavior, bug, or error labels Mar 15, 2017
@eryksun
Copy link
Contributor

eryksun commented Mar 15, 2017

-7 divided by 2 is -3, not -4

Integer division in Python is floor division, and it's self-consistent with the implementation of the modulo operation such that the following identity is satisfied: (a % n) == a - n * (a // n). For example:

(-7 % 2) == -7 - 2 * (-7 // 2)
       1 == -7 - 2 * (-4)
         == -7 + 8

This behavior is consistent with mathematical analysis languages such as MATLAB, Mathematica, Mathcad, and R. It's also consistent with Ruby, Perl, and Tcl. However, it's different from C, C++, C#, Java, PHP and many other languages. See the following Wikipedia article:

https://en.wikipedia.org/wiki/Modulo_operation

Please do not change the status and resolution of this issue again. This is not a bug.

@eryksun eryksun closed this as completed Mar 15, 2017
@eryksun eryksun added invalid type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 15, 2017
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants