473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Integer division, surprising results

As an old C programmer, I'm surprised by some results I'm getting with
integer division. For example:
-1/1000 -1 -9/2

-5

I expect the results of these expressions to be 0 and -4,
respectively.

I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?

Thanks,
Michael Cornelius
Jul 18 '05 #1
2 1819
> I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?


According to my math book integer division is defined as such:
a/b = q, where a = b*q + r and r is the remainder.

A remainder (ei a modulo) is always positive, effectively flooring all
the division operations.
Jul 18 '05 #2
Rory Geoghegan wrote:
I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?

According to my math book integer division is defined as such:
a/b = q, where a = b*q + r and r is the remainder.

A remainder (ei a modulo) is always positive, effectively flooring all
the division operations.


Remainders are always positive, but modulo can be
defined as either positive or negative (for negative
arguments). There are pros and cons for doing it either
way, although the Python way seems to have more pros
than cons.
--
Steven D'Aprano
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
2
by: Darius Fatakia | last post by:
hi, i'm new to MS Visual Studio and C++ but not to C programming in general. i'm trying to divide two integers and get their actual quotient (eg 5/3 = 1.666667 etc). i thought i had type cast...
3
by: Janice | last post by:
I got this question from my textbook and I cannot understand the theory. When a signed positive integer X divided by pow(2,k), the result is shifting k bits to right and putting w-k bits of 0 from...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
5
by: André | last post by:
Hello, I need to know whether the result of a division is a integer or not. dim x =60 dim y = 3 if x/y is a integer then .... else ....
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.