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


Groups > comp.lang.c > #66200

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Floating point to string, and back via `%a'...
Date 2015-07-22 17:18 -0400
Organization Self
Message-ID <55B00899.4030801@verizon.net> (permalink)
References <moorva$su7$1@speranza.aioe.org> <55AFFAEC.9060104@verizon.net> <moovlp$56p$1@speranza.aioe.org>

Show all headers | View raw


On 07/22/2015 04:49 PM, Chris M. Thomasson wrote:
>> "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.

So, what did it produce instead? Give the actual output from the program.

>>> 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

"%a" was added in C99. Does MSVC 12 claim C99 compatibility? I know that
MSVC has been extremely slow to adopt C99, but I've heard that recently
they've improved a bit. It's contrary to MS's business model to
encourage the writing of code that could be ported away from MS
platforms, so they have not made standard conformance a priority.

> 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

How can you tell? You didn't specify the precision in the %f format
string you used to print that value. The precision defaults to 6
(7.21.6.1p8), and 6 digits were printed out after the decimal point.
1.0123456789, rounded to 6 digits after the decimal point, is 1.012346,
so your output is perfectly consistent with the conversion having been
perfect. It could also be seriously imperfect - you didn't print enough
digits to tell.

Note: do NOT expect, if you do print enough digits, that you'll get
1.0123456789. That number cannot be represented exactly in any floating
point representation where FLT_RADIX is a power of 2, which is
overwhelming the most common case. The best you can hope for is that
"no" and "nc" represent the same exact value, one that is somewhat
different from 1.0123456789.

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