Double to string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    Double to string?

    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. Sinan Unur

    #2
    Re: Double to string?

    Rick <rrquick@nosp am-com> wrote in news:3fcea335$1 @clarion.carno. net.au:
    [color=blue]
    > 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.[/color]

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

    Sinan.

    --
    A. Sinan Unur
    asu1@c-o-r-n-e-l-l.edu
    Remove dashes for address
    Spam bait: mailto:uce@ftc. gov

    Comment

    • Ben Pfaff

      #3
      Re: Double to string?

      Rick <rrquick@nosp am-com> writes:
      [color=blue]
      > 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.[/color]

      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.)
      --
      "I hope, some day, to learn to read.
      It seems to be even harder than writing."
      --Richard Heathfield

      Comment

      • Rick

        #4
        Re: Double to string?

        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

        Ben Pfaff wrote:[color=blue]
        > Rick <rrquick@nosp am-com> writes:
        >
        >[color=green]
        >>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.[/color]
        >
        >
        > 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.)[/color]

        Comment

        • Ben Pfaff

          #5
          Re: Double to string?

          Rick <rrquick@nosp am-com> writes:
          [color=blue]
          > 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[/color]

          That's why I suggested reading an implementation of sprintf(),
          not using sprintf(). sprintf() supports double to string
          conversion with %e, %f, and %g.
          [color=blue]
          > Ben Pfaff wrote:[color=green]
          > > Rick <rrquick@nosp am-com> writes:
          > >[color=darkred]
          > >>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.[/color]
          > > 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.)[/color][/color]

          --
          char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
          ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x1f6}, *p=
          b,x,i=24;for(;p +=!*p;*p/=4)switch(x=*p& 3)case 0:{return 0;for(p--;i--;i--)case
          2:{i++;if(1)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

          Comment

          • Rahul

            #6
            Re: Double to string?

            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



            "Rick" <rrquick@nosp am-com> wrote in message
            news:3fcea335$1 @clarion.carno. net.au...[color=blue]
            > 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
            >[/color]


            Comment

            • those who know me have no need of my name

              #7
              Re: Double to string?

              in comp.lang.c i read:
              [color=blue]
              >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[/color]

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

              --
              a signature

              Comment

              • Rick

                #8
                Re: Double to string?

                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


                Comment

                • John Bode

                  #9
                  Re: Double to string?

                  Rick <rrquick@nosp am-com> wrote in message news:<3fcea335$ 1@clarion.carno .net.au>...[color=blue]
                  > 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[/color]

                  So sprintf() is out of the question?

                  Comment

                  • Dan Pop

                    #10
                    Re: Double to string?

                    In <3fcea335$1@cla rion.carno.net. au> Rick <rrquick@nosp am-com> writes:
                    [color=blue]
                    >How can I convert a double such as 23.1 to a string?[/color]

                    Are you sure you can have the value 23.1 represented by a double in the
                    first place?
                    [color=blue]
                    >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.[/color]

                    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
                    --
                    Dan Pop
                    DESY Zeuthen, RZ group
                    Email: Dan.Pop@ifh.de

                    Comment

                    • Mark McIntyre

                      #11
                      Re: Double to string?

                      On Thu, 4 Dec 2003 12:24:39 +0100, in comp.lang.c , "Rick"
                      <aso3rick@hotma il.com> wrote:
                      [color=blue]
                      >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?[/color]

                      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


                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


                      ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                      http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                      ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                      Comment

                      • J. J. Farrell

                        #12
                        Re: Double to string?

                        Rick <rrquick@nosp am-com> wrote in message news:<3fcea335$ 1@clarion.carno .net.au>...[color=blue]
                        >
                        > 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.[/color]

                        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?

                        Comment

                        • CBFalconer

                          #13
                          Re: Double to string?

                          Rick wrote:[color=blue]
                          > Ben Pfaff wrote:[color=green]
                          > > Rick <rrquick@nosp am-com> writes:
                          > >[color=darkred]
                          > >> 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.[/color]
                          > >
                          > > 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.)[/color]
                          >
                          > 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[/color]

                          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(unsigne d 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.

                          --
                          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                          Available for consulting/temporary embedded and systems.
                          <http://cbfalconer.home .att.net> USE worldnet address!

                          Comment

                          • August Derleth

                            #14
                            Re: Double to string?

                            "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in
                            news:Xns9446E4C 1ACAE0asu1corne lledu@132.236.5 6.8 on Wed 03 Dec 2003
                            08:29:14p:
                            [color=blue]
                            > Rick <rrquick@nosp am-com> wrote in
                            > news:3fcea335$1 @clarion.carno. net.au:
                            >[color=green]
                            >> 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.[/color]
                            >
                            > Don't know what avr-gcc and dtostrf are. However, have you considered
                            > sprintf?
                            >[/color]

                            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.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: Double to string?

                              August Derleth <libertarian232 003**@onewest.n et> writes:
                              [color=blue]
                              > snprintf isn't mentioned in K&R2, however, so it might not be part of
                              > Standard C.[/color]

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

                              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().
                              [color=blue]
                              > If you don't have access to a prewritten snprintf, it's trivial
                              > to write one yourself.[/color]

                              Really? Do you have a version that's better than using a
                              temporary file?
                              --
                              "I don't have C&V for that handy, but I've got Dan Pop."
                              --E. Gibbons

                              Comment

                              Working...