Simple atof() question
Doug Gwyn
gwyn at smoke.brl.mil
Thu Mar 21 07:19:29 AEST 1991
In article <1214 at caslon.cs.arizona.edu> armstron at cs.arizona.edu (Jim Armstrong) writes:
>How can I get atof() to return a float instead of a double?
You can't; it always returns type "double".
>When I run this simple program I get n = 37.549999 instead of
>n = 37.55. What am I doing wrong?
That's a different question. There's nothing particularly wrong with
your program, other than failing to return a value from main(). Note
that the float argument to printf() is promoted to a double, but that's
not a problem.
I think your real problem is that you think that the value 37.55 can
be represented exactly in a binary floating-point representation.
Somewhere along the way, probably within atof(), you're picking up a
small amount of "dirt" in the last bit or so of the internal
representation. This is usual for floating-point; you should learn
from this to not expect exactness when floating-point is involved.
More information about the Comp.lang.c
mailing list