473,385 Members | 1,673 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,385 software developers and data experts.

Help,how does division truncate?

I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
or -2,and -3%2 may be -1 and 1 respectively,it is implement-
dependent,so,is my understanding of this true(positive division and
mod is guaranteed while negative is not)? thx for your help in
advance..
Aug 18 '08 #1
7 6724
jackie wrote:
I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
or -2,and -3%2 may be -1 and 1 respectively,it is implement-
dependent,so,is my understanding of this true(positive division and
mod is guaranteed while negative is not)? thx for your help in
advance..
It depends on whether your implementation
conforms to C89 rules or C99 rules.

In C89, it is implementation defined:
If either operand is negative,
whether the result of the / operator is the largest integer
less than or equal to the algebraic quotient
or the smallest integer
greater than or equal to the algebraic quotient
is implementation-defined,
as is the sign of the result of the % operator.
In C99:
[#6] When integers are divided, the result of the / operator
is the algebraic quotient with any fractional part
discarded.78) If the quotient a/b is representable, the
expression (a/b)*b + a%b shall equal a.

78)This is often called ``truncation toward zero''.

--
pete
Aug 18 '08 #2
jackie wrote:
I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
or -2,and -3%2 may be -1 and 1 respectively,it is implement-
dependent,so,is my understanding of this true(positive division and
mod is guaranteed while negative is not)? thx for your help in
advance..
What you say was true for only in C90; in C99 section 6.5.5p6 says "When
integers are divided, the result of the / operator is the algebraic
quotient with any fractional part discarded.90)". The "90)" refers to
footnote 90, which says: 'This is often called ‘‘truncation toward zero"'.

What that means is that if the algebraic quotient is -1.5, the result -1
(the .5 is discarded). Since 1.5 gets rounded to 1, and -1.5 gets
rounded to -1, both rounded results are closer to 0 than the un-rounded
results, which is why this is referred to as "toward zero".

This causes problems for me. When I use negative numbers in such
calculations, what I want, far more often than not, is what they call
"rounding to negative infinity" - 1.5 gets rounded to 1, and -1.5 gets
rounded to -2 - both results are closer to negative infinity than the
number before rounding. However, standardizing it to round toward zero
still makes things a lot easier for me than not standardizing it at all.
Aug 18 '08 #3
pete wrote:
jackie wrote:
>I know in c that 3/2 gets 1 and 3%2 is also 1,and i think this is
guaranteed by C99(is that true?),but on the other hand,-3/2 may be -1
or -2,and -3%2 may be -1 and 1 respectively,it is implement-
dependent,so,is my understanding of this true(positive division and
mod is guaranteed while negative is not)? thx for your help in
advance..

It depends on whether your implementation
conforms to C89 rules or C99 rules.
He specified C99.
Aug 18 '08 #4
Thanks pete and James Kuyper,ur replies really make life easier for
me,actually,i am a student from China,and i don't have the opportunity
to have a copy of C99,so when i try to make the program more
portable,sometimes i really don't know what the standards are.So
really thank you for ur help here.

Aug 18 '08 #5
jackie wrote:
Thanks pete and James Kuyper,ur replies really make life easier for
me,actually,i am a student from China,and i don't have the opportunity
to have a copy of C99,so when i try to make the program more
portable,sometimes i really don't know what the standards are.So
really thank you for ur help here.
Google for n1256.pdf, in case Google censored that for you, try
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf

This the current C99 with TC1, TC2 and TC3 incorporated.

Bye, Jojo
Aug 18 '08 #6
Google for n1256.pdf, in case Google censored that for you, tryhttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

This the current C99 with TC1, TC2 and TC3 incorporated.

Bye, Jojo
thank u Joachim Schmitz,but what's the meaning of TC1,2,3? Is it still
a draft or it has already been approved? thx
Aug 19 '08 #7
jackie said:
>
>Google for n1256.pdf, in case Google censored that for you,
tryhttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

This the current C99 with TC1, TC2 and TC3 incorporated.

Bye, Jojo

thank u Joachim Schmitz,but what's the meaning of TC1,2,3?
TCs are "Technical Corrigenda", i.e. corrections to the text of the
Standard.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 19 '08 #8

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

Similar topics

3
by: martin | last post by:
Hi, We have a heavily used production server and a table which logs every hit on a web site. This table has grown large over time and we want to clear it down as efficiently as possible. We would...
27
by: Neil Monk | last post by:
(sorry for x-post but news://macromedia.dreamweaver seem unable to help, which I find astounding.) Hi, I'm VERY new to DW MX 2004 (opk then, I'm new to DW..period!) I'm sipmly trying to do the...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
2
by: Homer Simpson | last post by:
Hi everyone, I wrote a quick method to perform a calculation to determine the average of two numbers. When 23 and 40 were used, the result was 31, not 31.5! I discovered the problem, I was using...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
23
by: mlcampeau | last post by:
Hey guys, I have been mulling over this problem for a few days and have yet to come up with a query that will give me the expected results. I am working on a database that stores employee...
8
by: Kevin K | last post by:
Hey everyone, I'm new to python and am trying to do a little project with it. I'm running into problems writing over a file. I read from the file and loop through for a specfic case in which I...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.