473,396 Members | 1,599 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.

The Wisdom of 6.5.5(6)

As I was setting out from base camp to climb the "itoa mountain", full
of trepidation about crossing the "INT_MIN chasm", my wise old Sherpa
told me meditate on the meaning of section 6.5.5(6) of the C99 standard:

When integers are divided, the result of the / operator is the
algebraic quotient with any fractional part discarded.(87) If the
quotient a/b is representable, the expression (a/b)*b + a%b shall
equal a.

(87) This is often called "truncation toward zero".

To my shallow occidental eye, this seems to provide a solid definition
of both the / and % operators (division and modulisation?) for negative
integers, eliminating the traditional need to convert the integer being
formatted to its absolute value.

Question #1: Am I right about this?

Question #2: In what circumstances can "the quotient a/b" not be
representable? I can think of two:

* division by zero

* negative zero - only possible if a or b is negative
zero

Actually, I'm not sure that the second really qualifies as
not representable.

So, as usual, the real question is did I miss something?

Thanks!

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #1
4 1096
Ian Pilcher <i.*******@comcast.net> writes:
[...]
Question #2: In what circumstances can "the quotient a/b" not be
representable? I can think of two:

* division by zero

* negative zero - only possible if a or b is negative
zero
If b is negative zero, it's a division by zero. If a is negative zero
(and b is not), it's unspecified whether the result is negative zero
or plain zero; see C99 6.2.6.2.
Actually, I'm not sure that the second really qualifies as
not representable.


There's another case: overflow. For division, this can only happen
for a case like INT_MIN/(-1), and only if INT_MIN < -INT_MAX (i.e.,
for a two's-complement representation, which doesn't have a negative
zero).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2
Keith Thompson wrote:
Ian Pilcher <i.*******@comcast.net> writes:
[...]
Question #2: In what circumstances can "the quotient a/b" not be
representable? I can think of two:

* division by zero

* negative zero - only possible if a or b is negative
zero

If b is negative zero, it's a division by zero. If a is negative zero
(and b is not), it's unspecified whether the result is negative zero
or plain zero; see C99 6.2.6.2.

Actually, I'm not sure that the second really qualifies as
not representable.

There's another case: overflow. For division, this can only happen
for a case like INT_MIN/(-1), and only if INT_MIN < -INT_MAX (i.e.,
for a two's-complement representation, which doesn't have a negative
zero).


Sounds like I'm good as long as I'm dividing by a positive number.

Thanks!

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #3
Ian Pilcher wrote:
As I was setting out from base camp to climb the "itoa mountain",
full of trepidation about crossing the "INT_MIN chasm",


http://groups-beta.google.com/group/...25967a33eb82db

--
Peter

Nov 14 '05 #4
"Peter Nilsson" <ai***@acay.com.au> writes:
Ian Pilcher wrote:
As I was setting out from base camp to climb the "itoa mountain",
full of trepidation about crossing the "INT_MIN chasm",


http://groups-beta.google.com/group/...25967a33eb82db


Here's some code that I believe handles all values, works with both
C89 and C99 division rules, and is not sensitive to the relative
ranges of positive int's and negative int's.

Exercise for the obsessively-concerned-with-efficiency: modify this
function to use CPP functionality so that the expressions '(r>0)'
and/or '(r<0)' are optimized away on platforms where they are compile
time constants. (I believe the '(r<0)' expression in the 'else'
branch is always zero, but I included it for symmetry.)

void
portable_itoa( int n, char * s ){
char *p = s;
int r, t = n;

if( n < 0 ){
const char *digits = "9876543210987654321" + 9;
do *p++ = digits[ r = t%10 ]; while( t = t/10 + (r>0) );
*p++ = '-';
} else {
const char *digits = "1234567890123456789" + 9;
do *p++ = digits[ r = t%10 ]; while( t = t/10 - (r<0) );
}
*p = 0;

while( s < --p ) t = *s, *s++ = *p, *p = t;
}
Nov 14 '05 #5

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

Similar topics

0
by: Craig S. Ugoretz | last post by:
Dear newgroup readers, I am pleased to announce the presentation of the new open source software project I have created, called the "Wisdom Seeker IDE". For a description of what open source...
12
by: Franklin P Patchey | last post by:
I have modified some script and think i have put a bit in that isn't "compliant" Is the bit marked below (towards the end) correct - should it be () and not ("") <SCRIPT LANGUAGE="JavaScript">...
1
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the...
6
by: Filips Benoit | last post by:
Hi, A housebuiling firm has now 1 access-DB for the clients witch is used mostly bij remote desktop by only 1 person and it opens exclusively since now only one person is responsebly for...
10
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a...
4
by: Frank Rizzo | last post by:
In classic ASP, it was considered a bad idea to store VB6-created objects in the Application variable for various threading issues. What's the current wisdom on storing objects in the Application...
2
by: elein | last post by:
What is the current wisdom on setting the fsm variables for 8.0? How is it different from 7.4? Or is it? I am assuming these are the values that changed with Jan's changes. If not what were...
4
by: mdh | last post by:
Could I get some help understanding a concept that is related to exercise 4-9 in K&R II. The question relates to the properties of "EOF" and the issue of transformation from Char to Int. On page 43...
4
by: Jeff | last post by:
I need to set a minimum height for a div, yet have it expand with the contents. I have this from 2005: <style type="text/css"> div{ height: 12px; background-color: red; 1px solid...
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: 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: 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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.