Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Floating point to string, and back via `%a'... Date: Wed, 22 Jul 2015 17:08:20 -0700 Organization: None to speak of Lines: 83 Message-ID: References: <87mvyn51bm.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="28787"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19M2HINxwo6xJB4h2UxI3Df" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:yH57Irl4QKXV6hCzOSiMfgQ1MaE= sha1:rNdYSLg5V7HNUYmZqBI9/+BjPW8= Xref: csiph.com comp.lang.c:66224 Keith Thompson writes: > "Chris M. Thomasson" writes: > [...] >>> It looks like MSVC simply has a bug. You could calculate the number of >>> digits you need but that would be fiddly to get right in every single >>> case. >> >> Right. FWIW, %a does not work on MSVC for sscanf always returns zero. > > Does MSVC just not implement %a? Did you try changing to %la (you may > have already answered that). > > Try this test program: > > #include > int main(void) { > char buf[256]; > double in = 1.0123456789; > double out = 1.0123456789; > int result; > > sprintf(buf, "%a", in); > result = sscanf(buf, "%la", &out); > > printf("in = %.10f = %a\n", in, in); > printf("buf = \"%s\"\n", buf); > printf("sscanf returned %d\n", result); > printf("out = %.10f = %a\n", out, out); > > if (in == out) { > puts("in == out"); > } > else { > puts("in != out"); > } > return 0; > } Here's a better version. It doesn't examine the value of `out` unless sscanf succeeds. #include #include int main(void) { char buf[256]; double in = 1.0123456789; double out = FLT_MAX; /* arbitrary value */ int result; sprintf(buf, "%a", in); result = sscanf(buf, "%la", &out); printf("in = %.50f = %a\n", in, in); printf("buf = \"%s\"\n", buf); printf("sscanf returned %d\n", result); if (result == 1) { printf("out = %.50f = %a\n", out, out); if (in == out) { puts("in == out"); } else { puts("in != out"); } } return 0; } > On my system, the output is: > > in = 1.0123456789 = 0x1.0329161f1f56cp+0 > buf = "0x1.0329161f1f56cp+0" > sscanf returned 1 > out = 1.0123456789 = 0x1.0329161f1f56cp+0 > in == out > > What output do you get using MSVC? If it's significantly different, > there may well be a bug in MSVC (presumably in its runtime library). -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"