String standard

E

Evyn

Hi,

Can anyone tell me how C++ stores a string? I know that a double is
stored according to IEEE standard 754, sign bit, 11 bit exponent, 52
bit mantissa.

My task, essentially, is to write my own atof, so I thought I would
start with the structure of the string.

Regards (and thanks for your time),
Jim
 
A

Alf P. Steinbach

* Evyn:
Can anyone tell me how C++ stores a string? I know that a double is
stored according to IEEE standard 754, sign bit, 11 bit exponent, 52
bit mantissa.

A double is not necessarily stored according to IEEE standard.

You can use std::numeric_limits to check whether doubles are stored
according to IEEE standard with your compiler and compiler options.

My task, essentially, is to write my own atof, so I thought I would
start with the structure of the string.

You don't need to know the internal structure of a std::string.

Cheers, & hth.,

- Alf
 
J

Jonathan Lane

Hi,

Can anyone tell me how C++ stores a string? I know that a double is
stored according to IEEE standard 754, sign bit, 11 bit exponent, 52
bit mantissa.

My task, essentially, is to write my own atof, so I thought I would
start with the structure of the string.

If you mean a C-style string then it's just a null terminated char
array. if you mean a C++ style string then there isn't one. There's a
string class provided by the STL but there's no guarantees on how it's
implemented. If you want an atof take a look at the lexical parser in
boost.

What's wrong with atof that you need to roll your own?
 
E

Evyn

What's wrong with atof that you need to roll your own?

Thanks for the replies.

Nothing is wrong with it, the Prof. wants us to to roll our own, thus
we roll our own :)
 
O

osmium

Can anyone tell me how C++ stores a string? I know that a double is
stored according to IEEE standard 754, sign bit, 11 bit exponent, 52
bit mantissa.

My task, essentially, is to write my own atof, so I thought I would
start with the structure of the string.


The language specification says that IEEE 754 is "normative", which is a
convoluted way of saying " we wish it were so". Also, the rules for
primitive types, such as double, are different than those for classes in the
STL, so you started with a bad analogy.

You are not supposed to know about how a string is stored. One of the
*advantages* of C++ is that it is supposed to hide such information from
you. One of the shortcomings of C++ is the usual mechanism for handling
templates is that they sometimes, as an unwanted by-product, make the
internals of a class visible.

The public interface of string is sufficient to do what you want to do.
 
J

Jonathan Lane

Thanks for the replies.

Nothing is wrong with it, the Prof. wants us to to roll our own, thus
we roll our own :)

Right. In which case as the others here have said the public interface
should be sufficient. That is to say, you can treat any sort of string
implementation should be treatable as a char array via methods on the
public interface. Take a look at c_str() on std::string for instance
or operator[]. That should give you enough to get going.
 
P

Pete Becker

The language specification says that IEEE 754 is "normative", which is a
convoluted way of saying " we wish it were so".

"Normative" means "required." For example, each appendix in the
standard is marked as "normative" or as "informative." There's a
section early in the standard with the title "Normative references"
which has references to other standards whose contents are
incorporated, in whole or in part, in the C++ standard. IEEE 754 is not
one of those (nor is its counterpart, IEC 559).

I couldn't find anything that says that IEE 754 is "normative." Which
part are you referring to?
 
O

osmium

Pete Becker said:
"Normative" means "required." For example, each appendix in the standard
is marked as "normative" or as "informative." There's a section early in
the standard with the title "Normative references" which has references to
other standards whose contents are incorporated, in whole or in part, in
the C++ standard. IEEE 754 is not one of those (nor is its counterpart,
IEC 559).

I couldn't find anything that says that IEE 754 is "normative." Which part
are you referring to?

I can't reconstruct it, it was quite some time ago when I put that into my
head. I know my definition of "normative" came from searching through
dictionaries and such, and not from a standard. I will accept that you are
right. So it is a convoluted way of saying "required"?

I guess the bottom line is, is IEEE 754 the required representation for a
double, as the OP said?
 
P

Pete Becker

I can't reconstruct it, it was quite some time ago when I put that into my
head. I know my definition of "normative" came from searching through
dictionaries and such, and not from a standard. I will accept that you are
right. So it is a convoluted way of saying "required"?

Not convoluted said:
I guess the bottom line is, is IEEE 754 the required representation for a
double, as the OP said?

It's not required, but it has a special place. cf.
numeric_limits<Ty>::is_iec559, and the consequences of reporting true.
 
D

Default User

osmium said:
I can't reconstruct it, it was quite some time ago when I put that
into my head. I know my definition of "normative" came from
searching through dictionaries and such, and not from a standard.

I don't know dictionaries you were looking at, but none of the ones I
checked say anything like what you indicate.




Brian
 
O

osmium

Default User said:
I don't know dictionaries you were looking at, but none of the ones I
checked say anything like what you indicate.

"Of, relating to or prescribing a norm or standard:_normative grammar_" (1)

I took that to mean normal, standard, usual. As in "A person normally has
two arms". A person can have only one arm and still be a person, preferably
he has two.

(1) American Heritage Dictionary
 
D

Default User

osmium said:
"Of, relating to or prescribing a norm or standard:_normative
grammar_" (1)

I took that to mean normal, standard, usual. As in "A person
normally has two arms". A person can have only one arm and still be
a person, preferably he has two.

(1) American Heritage Dictionary

I don't interpret what you've quoted in the same way you do. When
"prescribing a standard" you aren't describing what is typical, but
what is required.




Brian
 
J

James Kanze

Right. In which case as the others here have said the public interface
should be sufficient. That is to say, you can treat any sort of string
implementation should be treatable as a char array via methods on the
public interface. Take a look at c_str() on std::string for instance
or operator[]. That should give you enough to get going.

The functions he'll doubtlessly need are those concerning
concatenation: + and += (both for string += string and string +=
char).

The prof sounds a bit sadic, however. Writing a correct
conversion (without rounding errors) to and from floating point
is extremely difficult.
 
E

Evyn

The prof sounds a bit sadic, however. Writing a correct
conversion (without rounding errors) to and from floating point is extremely difficult.


He can be!

Thanks for all the advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top