 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JS Guest
|
Posted: Mon Mar 14, 2005 9:25 pm Post subject: I don't understand this code. |
|
|
#include <ctype.h>
double atof(char s[]){
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++){
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
return sign * val / power;
}
The first for loop never terminates and I guess nothing is executed in its
body.
How should (s[i] - '0') be understood? Have never seen a char subtracted
from another char before.
JS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R Samuel Klatchko Guest
|
Posted: Mon Mar 14, 2005 11:54 pm Post subject: Re: I don't understand this code. |
|
|
JS wrote:
| Quote: | for (i = 0; isspace(s[i]); i++)
;
The first for loop never terminates and I guess nothing is executed in its
body.
|
Are you talking about the loop that I left above? It does terminate
when it finds a non-space character. It relies on the fact that the nul
character that terminates the string will never be a space.
| Quote: | How should (s[i] - '0') be understood? Have never seen a char subtracted
from another char before.
|
It's a trick that can be used in any character set where the code values
that represent the digits are in numeric order and are monotonically
increasing (i.e. if the code value for '0' is X, then the code value for
'1' is X+1, the code value for '2' is X+2, etc.). When this is the
case, you can figure out the numeric value of a digit character by
subtracting '0'.
samuel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Mon Mar 14, 2005 11:57 pm Post subject: Re: I don't understand this code. |
|
|
JS wrote:
| Quote: | for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
[...]
The first for loop never terminates and I guess nothing is executed in its
body.
|
What do you mean it never terminates? If you read it as:
Start at i = 0, go *while* the character at position i is space,
moving to the next position, you would understand that it does
stop -- it stops at the first non-space character (IOW, that
loop is there to skip any leading space characters -- including
space, tabs, and newlines, that is).
| Quote: | How should (s[i] - '0') be understood? Have never seen a char subtracted
from another char before.
|
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Mar 14, 2005 11:57 pm Post subject: Re: I don't understand this code. |
|
|
"JS" <sfsdfs (AT) asdas (DOT) com> writes:
| Quote: | #include
double atof(char s[]){
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++)
;
|
This loop skips leading spaces in s.
Note that isspace() (as well as isdigit()) requires its argument to
have an unsigned char value or be equal to EOF. Unless the
documentation of the function specifies this, this implement is buggy.
| Quote: | sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++){
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
return sign * val / power;
}
The first for loop never terminates and I guess nothing is executed in its
body.
|
You are right that the body is empty. The (possibly repeated) i++ is
the essence of the loop.
For what input do you see this loop never terminate?
| Quote: | How should (s[i] - '0') be understood? Have never seen a char subtracted
from another char before.
|
The expression computes the decimal value of s[i], relying on the
check (using isdigit()) that it is a decimal digit. This computation
depends on the guarantee that the decimal digits are encoded
contiguously, in increasing order in the execution character set.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dylan Nicholson Guest
|
Posted: Tue Mar 15, 2005 3:57 pm Post subject: Re: I don't understand this code. |
|
|
Carlos Moreno <moreno_at_mochima_dot_com (AT) xx (DOT) xxx> wrote
| Quote: |
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
Except the OP never said they were working on an ASCII platform. |
Mind you, this is safe for EBCDIC too, and I don't know how many
real-world platforms exist for which character values for '0' to
'9' are not consecutive and contiguous.
But I've certainly been bitten by code (especially GNU code) that
happily assumes the whole world uses ASCII.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Tue Mar 15, 2005 3:57 pm Post subject: Re: I don't understand this code. |
|
|
Carlos Moreno wrote:
| Quote: | Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
|
Please don't spread any more the wrong myth that the C++ language is
coupled with a specific character set or character encoding such as ASCII.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Mar 15, 2005 8:42 pm Post subject: Re: I don't understand this code. |
|
|
Seungbeom Kim wrote:
| Quote: | Carlos Moreno wrote:
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
Please don't spread any more the wrong myth that the C++ language is
coupled with a specific character set or character encoding such as ASCII.
|
Well, okay, but the underlying assertion, that you can subtract '0' from
the character representation of a digit to get the numeric value that
the character represents is true. That's required by the C and C++
standards.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Mar 15, 2005 8:43 pm Post subject: Re: I don't understand this code. |
|
|
In article <4NnZd.54162$WM1.934982 (AT) wagner (DOT) videotron.net>, Carlos Moreno
<moreno_at_mochima_dot_com (AT) xx (DOT) xxx> writes
| Quote: | Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
|
Actually the C and C++ Standards require the digits have consecutive
codes in numerical order regardless of what coding is used.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Tue Mar 15, 2005 9:05 pm Post subject: Re: I don't understand this code. |
|
|
Dylan Nicholson wrote:
| Quote: | Carlos Moreno <moreno_at_mochima_dot_com (AT) xx (DOT) xxx> wrote
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
Except the OP never said they were working on an ASCII platform.
|
I *knew it*!! I knew someone was going to bring that up! :-)
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Wed Mar 16, 2005 9:13 am Post subject: Re: I don't understand this code. |
|
|
Pete Becker wrote:
| Quote: | Seungbeom Kim wrote:
Carlos Moreno wrote:
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
Please don't spread any more the wrong myth that the C++ language is
coupled with a specific character set or character encoding such as ASCII.
Well, okay, but the underlying assertion, that you can subtract '0' from
the character representation of a digit to get the numeric value that
the character represents is true. That's required by the C and C++
standards.
|
... which is true even without assuming ASCII.
You seem to refute something that I never said.
(Did I say anywhere that the assumption you mentioned was false? )
I want to emphasize that these are two totally different things:
(1) Digits have consecutive codes because it's guaranteed by ASCII, which
we are/happen to be using.
(2) Digits have consecutive codes because it's guaranteed by C/C++,
regardless of the character set/character encoding used (as long as it's
compatible with the requirement of C/C++, of course).
Carlos Moreno's hint certainly seemed to imply (1), and I wanted to say
that it's not true.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dylan Nicholson Guest
|
Posted: Thu Mar 17, 2005 2:08 am Post subject: Re: I don't understand this code. |
|
|
Pete Becker <petebecker (AT) acm (DOT) org> wrote
| Quote: | Seungbeom Kim wrote:
Carlos Moreno wrote:
Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
Please don't spread any more the wrong myth that the C++ language is
coupled with a specific character set or character encoding such as ASCII.
Well, okay, but the underlying assertion, that you can subtract '0' from
the character representation of a digit to get the numeric value that
the character represents is true. That's required by the C and C++
standards.
|
Really? What would be the point of such a requirement? Obviously any
*given* implementor can make use of this, but I can't think of a
reason to restrict other implementors from using character values that
do not work like this.
For instance, I might want to assign values based on (US) keyboard
position:
'~' == 1
'`' == 2
'!' == 3
'1' == 4
'@' == 5
'2' == 6
etc.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Mar 17, 2005 8:06 am Post subject: Re: I don't understand this code. |
|
|
Dylan Nicholson wrote:
| Quote: | Carlos Moreno <moreno_at_mochima_dot_com (AT) xx (DOT) xxx> wrote in
message news:<4NnZd.54162$WM1.934982 (AT) wagner (DOT) videotron.net>...
Hint: '0' is the ASCII code of the character 0... Given
that the ASCII codes of all the digits are consecutive, what
would you get when you subtract the ASCII code of a digit X
minus the ASCII code of 0?
Except the OP never said they were working on an ASCII
platform. Mind you, this is safe for EBCDIC too, and I don't
know how many real-world platforms exist for which character
values for '0' to '9' are not consecutive and contiguous.
|
The C and the C++ standards require it. They make a (very) few
guarantees concerning the character set, however:
-- All of the basic source characters (legal characters in a
C++ source), plus 'a', 'b', 'r' and ' ', must be
present. The ' ' is the only required code point; it must
be 0x00. (I have seen character sets where 0x00 represented
the digit '0' or the space character, but such a character
set cannot be used within C or C++ in a conforming
implementation.)
-- The numerical values of all characters in the basic
character set must be positive. Thus, char's on a machine
using EBCDIC must either be unsigned, or have more than 8
bits.
Note that this guarantee does NOT extend to characters
outside the basic character set, much to the dismay of those
of us who regularly use such characters. (There are some in
my address, for example.)
-- The encoding for a decimal digit can be obtained by adding
the value of the digit to '0'.
Note that this only holds for decimal digits. There is no
requirement that upper or lower case alphabetic characters
be contiguous, or even in alphabetical order.
| Quote: | But I've certainly been bitten by code (especially GNU code)
that happily assumes the whole world uses ASCII.
|
You mean things along the lines of "if ( ch>='a' && ch<='z' )"?
There was a time when such code was frequent. Today, however, I
wonder. It doesn't work in any of the most frequently used
single byte codesets (ISO 8859-n). And it doesn't work with any
of the multibyte codesets (UTF- nor any of the wchar_t
codesets that I know of.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Mar 17, 2005 11:46 am Post subject: Re: I don't understand this code. |
|
|
Carlos Moreno wrote:
| Quote: | Hint: '0' is the ASCII code of the character 0... Given that
the ASCII codes of all the digits are consecutive, what would
you get when you subtract the ASCII code of a digit X minus
the ASCII code of 0?
|
Carlos should have used a phrase such as "character encoding" instead
of the phrase "ASCII code." I suspect that he understood the
distinction but chose to simplify the discussion, perhaps assuming that
the OP was a beginner. This seems safe to me because:
* There's a good chance that the OP IS using ASCII;
* If the OP is NOT using ASCII, he might be blissfully unaware of
this;
* If the OP understands that he is not using ASCII, then he still
might
understand (perhaps naively) that this also applies to other
character
sets (but even the naive assumption would, in this particular case,
happen to lead him to the correct conclusions).
Seungbeom Kim wrote:
| Quote: | Please don't spread any more the wrong myth that the C++ language is
coupled with a specific character set or character encoding such as
ASCII. |
You chose to believe that Carlos's assumptions were imprecise, rather
than his exact wording... that Carlos seems to believe that all C++
implementations use ASCII. I don't see any evidence of this, but it's
not unreasonable.
Pete Becker wrote:
| Quote: | Well, okay, but the underlying assertion, that you can subtract '0'
from
the character representation of a digit to get the numeric value that
the character represents is true. That's required by the C and C++
standards.
|
Pete Becker neatly steps around the whole issue of what character set
the OP uses -- he points out that the important assumption about
character sets is true, no matter which character set you are using.
Seungbeom Kim wrote:
| Quote: | I want to emphasize that these are two totally different things:
(1) Digits have consecutive codes because it's guaranteed by ASCII,
which
we are/happen to be using.
(2) Digits have consecutive codes because it's guaranteed by C/C++,
regardless of the character set/character encoding used (as long as
it's
compatible with the requirement of C/C++, of course).
|
Agreed, these are two, totally different things.
| Quote: | Carlos Moreno's hint certainly seemed to imply (1), and I wanted to
say
that it's not true.
|
Assume you do NOT mean that ASCII digits are not consecutive! You're
just trying to say that assumption (1) isn't as relevant as assumption
(2), because if you do NOT happen to be using ASCII, assumption (2)
still holds.
In this you seem to be making exactly the same mistake that you accuse
Carlos of making -- being less than 100% precise with your words.
At any rate, I think that all of these points about character sets
(ASCII not required, but subtracting '0' works correctly in any legal
character set) are now excruciatingly clear to everybody.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Thu Mar 17, 2005 11:54 am Post subject: Re: I don't understand this code. |
|
|
Seungbeom Kim wrote:
| Quote: | I want to emphasize that these are two totally different things:
(1) Digits have consecutive codes because it's guaranteed by ASCII, which
we are/happen to be using.
(2) Digits have consecutive codes because it's guaranteed by C/C++,
regardless of the character set/character encoding used (as long as it's
compatible with the requirement of C/C++, of course).
Carlos Moreno's hint certainly seemed to imply (1), and I wanted to say
that it's not true.
|
Well, I just figured the hint would be simpler to understand by
sticking to the simple assumption, and not going into depths of
various char encodings... In retrospect, I guess I could have
simply said something like: '0' is the internal code used to
represent the character 0 -- could be the ASCII code of 0, for
instance -- and given that the codes for the various digits are
consecutive..... etc. etc." (mainly given that I saw all this
coming while writing my previous message and deciding to "keep
it simple" -- yeah, like that is even possible in c.l.c.m )
Cheers,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Mar 17, 2005 4:00 pm Post subject: Re: I don't understand this code. |
|
|
Dylan Nicholson wrote:
| Quote: | Except the OP never said they were working on an ASCII platform.
Mind you, this is safe for EBCDIC too, and I don't know how many
real-world platforms exist for which character values for '0' to
'9' are not consecutive and contiguous.
But I've certainly been bitten by code (especially GNU code) that
happily assumes the whole world uses ASCII.
|
What IS different for EBCDIC is that:
1. The letters aren't contiguous. Owing to it's "card processing"
history, there's gaps between I and J and between R and S. (Junior
is 11 if you're old enough).
2. The relative locations of the upper case, lower case, and digits
are different, so simple sort by value will give different ordering.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|