0

Can you explain what's happening with python shell..

>>> 6/7   
0

>>> -6/7   
-1

>>> -(6/7)    
0
2

2 Answers 2

1

With the / operator python always rounds to minus infinity (so to the "more negative" value) if you input integers, like stated in the python docs. This explains the described behavior.

So 6/7 would be 0.857... and gets rounded to 0 while -6/7 gives -0.857... and will be rounded to -1. Finally -0 equals 0.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to perform floating point division you should set the following import at the top of your script or as the first line in your Python shell:

from __future__ import division

This will ensure that you get proper results. If you want to perform integer division use // instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.