Bug 1668 – std.stream readf can't read int, nan, inf as floats

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-11-13T18:51:00Z
Last change time
2014-02-16T09:22:04Z
Assigned to
rbanderton
Creator
wbaxter

Comments

Comment #0 by wbaxter — 2007-11-13T18:51:57Z
std.stream readf can't read int, nan, inf as floats. This means that readf can't read in everything that writef prints out, even if you know the exact order of elements in the stream. Here are some tests: //=========================================================================== module rwnans; import std.stdio; import std.stream; void try_rwop(float x, string fmt="") { writefln("--- Test x=%f", x); auto S = new MemoryStream(); S.writef(fmt, x); S.position = 0; writefln("S buffer contains '%s'", S.readLine()); S.position = 0; float y; int got = S.readf(&y); writefln("read %d bytes, got y = %s", got, y); } void main() { try_rwop(1.0); try_rwop(1.0,"%f"); float x; // nan try_rwop(x); try_rwop(x,"%f"); x = 1.0/0.0 ; // inf try_rwop(x); try_rwop(x,"%f"); try_rwop(-x); try_rwop(-x,"%f"); }
Comment #1 by wbaxter — 2007-11-13T19:38:11Z
More info. The failure to parse something like "1" as a float seems to only happen when there's only a single byte in the stream. And then parsing as an int fails too. Another bug: if you rewind the stream after failing to parse that as a float, then the next attempt to parse as an int will get two characters instead of the one and only one that was in the stream to begin with. Here's an updated test program: //=========================================================================== module rwnans; import std.stdio; import std.stream; float try_rwop(float x, string fmt="", string pad="") { writefln("--- Test x=%f", x); auto S = new MemoryStream(); S.writef(fmt, x, pad); S.position = 0; writefln("S buffer contains '%s'", S.readLine()); S.position = 0; float y; int got; do { got = S.readf(&y); writefln("read %d bytes, got y = %s", got, y); if (got) break; S.position=0; // try as integer int iy; got = S.readf(&iy); writefln("read %d bytes as int, got iy = %s", got, iy); if(got) break; S.position=0; // try nan inf,-inf got = S.readf("nan"); if (got) break; S.position=0; got = S.readf("-inf"); if (got) break; S.position=0; got = S.readf("inf"); if (got) break; } while(false); if (!got) writefln("Couldn't parse it!"); else writefln("finally got y=%s", y); return y; } void main() { string pad = " "; try_rwop(1.0); // fails! try_rwop(1.0,"%f"); // ok try_rwop(1.0,"",pad);// ok try_rwop(1.0,"%f"); try_rwop(12.0); // ok try_rwop(12.0,"%f"); // ok try_rwop(1.001); // ok try_rwop(1.001,"%f");// ok float x; // nan try_rwop(x); // fails! try_rwop(x,"%f"); // fails! try_rwop(x,"%f",pad);// fails! x = 1.0/0.0 ; // inf try_rwop(x); // fails! try_rwop(x,"%f"); // fails! try_rwop(x,"%f",pad);// fails! try_rwop(-x); // fails! try_rwop(-x,"%f"); // fails! try_rwop(x,"%f",pad);// fails! }
Comment #2 by andrei — 2014-02-11T19:15:37Z
@Blake thanks for working on this!
Comment #3 by rbanderton — 2014-02-13T07:27:07Z
Comment #4 by github-bugzilla — 2014-02-16T09:19:36Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f476f47bab3f2e92bac1f37d9f2e3ea23b78ef5a Fixed Issue 1668 std.stream readf can't read int, nan, inf as floats https://github.com/D-Programming-Language/phobos/commit/a56882e036087083fcc29444ec4d2d36bf5088d2 Merge pull request #1929 from rbanderton/fix-issue-1668 Fix issue 1668 - std.stream readf can't read int, nan, inf as floats
Comment #5 by andrej.mitrovich — 2014-02-16T09:22:04Z
As has been said Phobos D1 bugs won't be fixed, so the pull was meant for D2.