Article View: comp.sys.mac
Article #4136Scanf bugfix for Think C 4.0
From: John Peterson
Date: Wed, 06 Sep 1989 23:57
Date: Wed, 06 Sep 1989 23:57
55 lines
1953 bytes
1953 bytes
In playing with Think C 4.0, I've found a couple of bugs in their
scanf routines for floating point. First, it drops leading zeros
occuring after "0.", e.g., "0.001234" gets read as "0.1234". Second,
if you input a string of more than 20 digits, it starts dividing the
result by ten for every extra digit.
The following patch appears to correct these problems. Note I haven't
tested these fixes extensivly (although I've apparently tested them more
than Think did...)
In routine _vfscanf (in C Libraries:sources:scanf.c) change the code
fragment:
-----------
/* (floating) process each digit */
case -2:
if (c >= '0' && c <= '9') {
F.valid = TRUE;
if (c != '0' || D.sig[0]) {
if (D.sig[0] < sizeof D.sig - 1)
D.sig[++D.sig[0]] = c;
if (F.dot)
--D.exp;
}
}
to look like this:
-----------
/* (floating) process each digit */
case -2:
if (c >= '0' && c <= '9') {
F.valid = TRUE;
if (c != '0' || D.sig[0]) {
if (D.sig[0] < sizeof D.sig - 1) {
D.sig[++D.sig[0]] = c;
/* Bugfix JWP - Don't bump exp for ignored digits */
if (F.dot)
--D.exp;
}
}
/* Bugfix JWP - Don't ignore zeros after "0." */
if (c == '0' && (! D.sig[0]))
if (F.dot)
--D.exp;
}
Then rebuild the various "ANSI" projects (note the configuration files
contained in the "config" directory.) Thank heaven for sources.
Cheers,
John "so That's why my code broke" Peterson
[Disclaimer: This posting is not endorsed or supported by Apple Computer Inc.]
Technical Details
Message-ID:
<34559@apple.Apple.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!bionet!apple!jpShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!bionet!apple!jp From: jp@Apple.COM (John Peterson) Newsgroups: comp.sys.mac Subject: Scanf bugfix for Think C 4.0 Keywords: floating point Think C scanf Message-ID: <34559@apple.Apple.COM> Date: 6 Sep 89 23:57:25 GMT Organization: Apple Computer Inc., Cupertino, CA Lines: 55