Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #66196

Re: Floating point to string, and back via `%a'...

From "Chris M. Thomasson" <nospam@nospam.nospam>
Newsgroups comp.lang.c
Subject Re: Floating point to string, and back via `%a'...
Date 2015-07-22 13:49 -0700
Organization Aioe.org NNTP Server
Message-ID <moovlp$56p$1@speranza.aioe.org> (permalink)
References <moorva$su7$1@speranza.aioe.org> <55AFFAEC.9060104@verizon.net>

Show all headers | View raw


> "James Kuyper"  wrote in message news:55AFFAEC.9060104@verizon.net...

> > On 07/22/2015 03:46 PM, Chris M. Thomasson wrote:
> > How in the heck can I use the `%a' format specifier in
> > conjunction with sprintf/sscanf so that I can convert a
> > floating point to a string, and back? I have some code,
> > but it does not work with `%a'. Damn!

> It would help if you identified how it failed to work. Which compiler
> are you using? What options did you select when compiling? Did
> compilation fail with error messages? If so, provide those messages. Did
> it not do what you expected it to do when you ran it? If so, tell us
> what it actually did.

I am using MSVC 12. Default for a new project. No error message from the 
compiler. It failed to convert a string populated with sprintf and %a back 
to the original float.


> > Also, why does it seem to work for a %f %lf sprintf to sscanf pair?

> Because of the 'l'.
…

I think I tried that and it still did not work with %a on MSVC. It seems to 
work on GCC, but does not seems to get the precision correct:

I just quickly typed this in...

http://codepad.org/yO2UisHn

__________________________________________________
    /* try to use default precision `%a' added `%la' with sprintf/sscanf */
    {
        sprintf(buf, "%.10la", no);

        double nc = 0.0;
        int status = sscanf(buf, "%la", &nc);

        if (! status || status == EOF)
        {
            printf("sscanf `%%a' error!\n");
        }

        printf("(%%s):buf = %s\n", buf);
        printf("(%%a):nc = %f\n\n", nc);
    }
__________________________________________________


This gives:
__________________________________________________
(%s):buf = 0x1.0329161f1fp+0
(%a):nc = 1.012346
__________________________________________________



I am missing a lot of accuracy here from:
1.0123456789

;^O



How am I messing things up now damnit!?

;^O



> >         double nc = 0.0;
> >         int status = sscanf(buf, "%a", &nc);

> "%a" is for float. Use "%la" for double (7.21.6.2p11). Similarly, you
> should be using "%lf", not "%f". This applies to all of your sscanf()
> calls. The 'l' is unnecessary for printf() and sprintf(), because floats
> are automatically promoted to double when the type of the corresponding
> argument of a function has not been declared in a function prototype.
> However, it is also permitted, to allow the same format string to be
> used for both sprintf() and sscanf().

…

Thank you!



> >         int status = sscanf(buf, "%.10a", &nc);

> sscanf() does not allow you to specify the precision, and '.' is not a
> permitted character in sscanf() directives.

DOH! Thanks again James!

;^o 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 12:46 -0700
  Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 16:19 -0400
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 13:49 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:01 -0700
      Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 17:18 -0400
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:22 -0700
  Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 13:40 -0700
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:01 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:12 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:20 -0700
          Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:24 -0700
          Re: Floating point to string, and back via `%a'... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-22 21:30 +0000
            Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:39 -0700
          Re: Floating point to string, and back via `%a'... Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-07-22 19:49 -0600
  Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-22 22:28 +0100
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:34 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:58 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:43 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:45 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:51 -0700
          Re: Floating point to string, and back via `%a'... Robert Wessel <robertwessel2@yahoo.com> - 2015-07-22 18:21 -0500
            Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:58 -0700
        Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:52 -0700
        Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 17:08 -0700
      Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 00:01 +0100
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 19:40 -0700
          Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 22:32 -0700
          Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:07 +0100
            Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:25 +0100
              Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:44 -0700
            Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:47 -0700

csiph-web