A question: Convert double to string

A

Aman JIANG

If you really want a custom solution, you should be able to find
some open source implementation to start from... look
for dtoa() or fcvt() as common low-level function names.

I cannot find an open source of fcvt() function implementation ...
 
K

Kai-Uwe Bux

Aman said:
Yes, I have this problem. But I think that 'vsnprintf' of standard C++
with a simple wrap can provides the same function, right ?

The function vsnprintf() is not in the current standard. But it is in the
draft for the next revision.

This is the wrapper that I added to my library:

bool strprintf ( std::string & buffer, char const * format, ... ) {
while ( true ) {
buffer.resize( buffer.capacity() );
int old_length = buffer.size();
std::va_list aq;
va_start( aq, format );
int length_needed =
vsnprintf( &buffer[0], old_length + 1, format, aq );
va_end( aq );
if ( length_needed < 0 ) {
return ( false );
}
buffer.resize( length_needed );
if ( length_needed <= old_length ) {
return ( true );
}
}
}

Note that this has formally undefined behavior since it assumes (a) that
std::string is contiguous (this is in the current working draft) and (b)
that one can safely write a '\0' at buffer[ buffer.size() ] (works for the
STL implementation I am using).


Best

Kai-Uwe Bux
 
I

Ivan Vecerina

: On Sep 23, 2:37 am, "Ivan Vecerina"
:
: > If you really want a custom solution, you should be able to find
: > some open source implementation to start from... look
: > for dtoa() or fcvt() as common low-level function names.
:
: I cannot find an open source of fcvt() function implementation ...

fcvt() is in the documentation of the GNU C library, and has
been a BSD/POSIX function ... try asking in a related newsgroup.

Oh, and I have come upon this, you might be interested in it:
http://code.google.com/p/stringencoders/
although I have not been impressed by the actual code:
http://stringencoders.googlecode.com/svn/trunk/src/modp_numtoa.c
 
A

Aman JIANG

The function vsnprintf() is not in the current standard. But it is in the
draft for the next revision.


God... I have no idea... Is that true? I found this on MSDN:
http://msdn2.microsoft.com/en-us/library/1kt27hek(VS.80).aspx
There in "Compatibility" column, it had a "ANSI" word,
I think it means C89 ? Am I wrong ?

This is the wrapper that I added to my library:

bool strprintf ( std::string & buffer, char const * format, ... ) {
while ( true ) {
buffer.resize( buffer.capacity() );
int old_length = buffer.size();
std::va_list aq;
va_start( aq, format );
int length_needed =
vsnprintf( &buffer[0], old_length + 1, format, aq );
va_end( aq );
if ( length_needed < 0 ) {
return ( false );
}
buffer.resize( length_needed );
if ( length_needed <= old_length ) {
return ( true );
}
}
}

Note that this has formally undefined behavior since it assumes (a) that
std::string is contiguous (this is in the current working draft) and (b)
that one can safely write a '\0' at buffer[ buffer.size() ] (works for the
STL implementation I am using).

I found this:
http://perfec.to/vsnprintf/
 
K

Kai-Uwe Bux

Aman said:
God... I have no idea... Is that true?
Yes.

I found this on MSDN:
http://msdn2.microsoft.com/en-us/library/1kt27hek(VS.80).aspx
There in "Compatibility" column, it had a "ANSI" word,
I think it means C89 ? Am I wrong ?

No idea. Maybe, you should ask Microsoft about what they mean.

This is the wrapper that I added to my library:

bool strprintf ( std::string & buffer, char const * format, ... ) {
while ( true ) {
buffer.resize( buffer.capacity() );
int old_length = buffer.size();
std::va_list aq;
va_start( aq, format );
int length_needed =
vsnprintf( &buffer[0], old_length + 1, format, aq );
va_end( aq );
if ( length_needed < 0 ) {
return ( false );
}
buffer.resize( length_needed );
if ( length_needed <= old_length ) {
return ( true );
}
}
}

Note that this has formally undefined behavior since it assumes (a) that
std::string is contiguous (this is in the current working draft) and (b)
that one can safely write a '\0' at buffer[ buffer.size() ] (works for
the STL implementation I am using).

I found this:
http://perfec.to/vsnprintf/

Now, that _is_ disconcerting. I was assuming that vsnprintf conforms to C99.
I think, when C++ incorporates vsnprintf into the standard, it will go for
compatibility.

You should check your library for conformance.


Best

Kai-Uwe Bux
 

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
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top