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 16:52:23 -0700 Organization: None to speak of Lines: 48 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="U2FsdGVkX18c1Q3OIYCESHzeZKwkHDtX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:XBnyvBn78W4eAza8HPIblmk1638= sha1:5jOMcyAF1AL/h0EjNWZgK0zweRU= Xref: csiph.com comp.lang.c:66222 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; Sorry, I should have left `out` uninitialized, or initialized it to some other value (it was a copy-and-paste error). > 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; > } [...] -- 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"