Double to string?

R

Rick

Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Rick
 
A

A. Sinan Unur

Rick said:
Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Don't know what avr-gcc and dtostrf are. However, have you considered
sprintf?

Sinan.
 
B

Ben Pfaff

Rick said:
How can I convert a double such as 23.1 to a string? I don't want to
use any library functions because the code is being written in avr-gcc
and I don't like the way dtostrf() has been implemented. Thanks.

I would refer to P.J. Plauger, _The Standard C Library_.
Alternatively, read the GNU libc implementation of sprintf (but
you won't like it.)
 
R

Rick

I can't use sprintf because it's not supported by the version of avr-gcc
(it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks


Rick
 
B

Ben Pfaff

Rick said:
I can't use sprintf because it's not supported by the version of
avr-gcc (it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks

That's why I suggested reading an implementation of sprintf(),
not using sprintf(). sprintf() supports double to string
conversion with %e, %f, and %g.
 
R

Rahul

You can use the following logic:
1. Extract each digit of the number
2. Find the ascii equivalent of each number using a look up table [0..9]
3. Create a string

RS
 
T

those who know me have no need of my name

in comp.lang.c i read:
I can't use sprintf because it's not supported by the version of
avr-gcc (it's a compiler). I'm only interested in some "manual" way of
efficiently carrying a double to string conversion. Thanks

the point to your reading an existing implementation is so you can gain an
understanding of how it can be accomplished.
 
R

Rick

Hi,

How confusing, I'm another Rick but I have the same question :) However, I
was thinking about the same way to convert... but howto extract a digit from
an integer, float?

Greetings,
Rick
 
J

John Bode

Rick said:
Hi,

How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Rick

So sprintf() is out of the question?
 
D

Dan Pop

In said:
How can I convert a double such as 23.1 to a string?

Are you sure you can have the value 23.1 represented by a double in the
first place?
I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

If you're dealing with reasonable values (i.e. you don't have to resort to
the exponential format), it's quite straightforward.

Deal with the sign first and make the value positive if it was negative.
If the number is greater or equal to 1, keep dividing it by 10 until it
no longer is and count the number of divisions. Now, perform the
following simple steps:

1. If the division count is zero, insert the decimal point in the string.
2. Decrement the division count.
3. Multiply the value by 10.
4. Convert the integer part of the result to a digit code and insert it
into the string.
5. The fractional part becomes the new value you're dealing with.
6. If the division count is equal to the negative of the number of desired
decimals, insert the null terminator and you're done.
7. Go to step 1.

It may not be the fastest or the most accurate algorithm, but it is the
easiest to implement with no library support.

Dan
 
M

Mark McIntyre

Hi,

How confusing, I'm another Rick but I have the same question :) However, I
was thinking about the same way to convert... but howto extract a digit from
an integer, float?

This is an algorithm question

first consider that in C, you're guaranteed that '1'-'0' == 1
then consider how to determine the number of tens and units in 43,
using integer division
 
J

J. J. Farrell

Rick said:
How can I convert a double such as 23.1 to a string? I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.

Assuming that dtostrf() converts a double to a string, the obvious
thing would seem to be to start from that and re-implement it in a
way that you like. Why does a dislike of the implementation of one
function stop you using other library functions?
 
C

CBFalconer

Rick said:
I can't use sprintf because it's not supported by the version
of avr-gcc (it's a compiler). I'm only interested in some
"manual" way of efficiently carrying a double to string
conversion. Thanks

Don't toppost. Fixed this time.

Work on the longest integer type available to you, which will be
unsigned. The least significant decimal digit of this is
available as:

(value % 10) + '0';

You can extract all digits with a simple function such as:

void extract(unsigned value)
{
if (value / 10) extract(value / 10);
putchar('0' + value % 10);
}

Going from double to that depends on the binary structure of a
double, and is not really portable. You have to figure out how to
extract the necessary elements. float.h will help.
 
A

August Derleth

29:14p:
Don't know what avr-gcc and dtostrf are. However, have you considered
sprintf?

For safety's sake, use snprintf if you can: sprintf will smash a buffer
that's too small, writing on memory you don't actually own. snprintf takes
the size of the buffer as an extra argument, and so will not try to access
unallocated memory.

snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C. gcc, however, implements it, and it is a widespread extension
in any case. If you don't have access to a prewritten snprintf, it's
trivial to write one yourself.
 
B

Ben Pfaff

August Derleth said:
snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C.

snprintf() is in C99, although its return value is different from
what (some?) pre-standard implementations used.
gcc, however, implements it, and it is a widespread extension
in any case.

Actually GCC doesn't come with a C library, so it may or may not
be available with GCC. The GNU C library does include
snprintf().
If you don't have access to a prewritten snprintf, it's trivial
to write one yourself.

Really? Do you have a version that's better than using a
temporary file?
 
D

Dan Pop

In said:
29:14p:


For safety's sake, use snprintf if you can: sprintf will smash a buffer
that's too small, writing on memory you don't actually own. snprintf takes
the size of the buffer as an extra argument, and so will not try to access
unallocated memory.

snprintf isn't mentioned in K&R2, however, so it might not be part of
Standard C. gcc, however, implements it, and it is a widespread extension
in any case. If you don't have access to a prewritten snprintf, it's
trivial to write one yourself.

What part of "I don't want to use any library functions" was too
difficult for all of you to understand?

If the OP is writing code for a freestanding implementation, he may not
have a <stdio.h> (and the corresponding library support) in the first
place.

Dan
 

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

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top