[comp.lang.c] Simple atof

armstron@cs.arizona.edu (Jim Armstrong) (03/19/91)

How can I get atof() to return a float instead of a double? 
When I run this simple program I get n = 37.549999 instead of
n = 37.55.  What am I doing wrong?


#include <stdio.h>

main()
{

double atof();
char buf[10];
float n;

strcpy(buf,"37.55");
n = (float) atof(buf);
printf("n = %f\n",n);

}


-- 
Jim Armstrong			  "The nonpayment and subsequent abuse of
armstron@cs.arizona.edu		  socially powerless athletes is simply a
{uunet|noao}!arizona!armstron     form of modern-day slavery" --Rick Telander

brendan@cs.widener.edu (Brendan Kehoe) (03/20/91)

In <1214@caslon.cs.arizona.edu>, armstron@cs.arizona.edu writes:
>How can I get atof() to return a float instead of a double? 
>When I run this simple program I get n = 37.549999 instead of
>n = 37.55.  What am I doing wrong?
>[...]
>n = (float) atof(buf);
>printf("n = %f\n",n);
>[...]

  I have a feeling it's got something to do with the way printf takes
  its argument .. notice if ya do
	printf("n = %.2f\n", n);
  you do get 37.55.

-- 
     Brendan Kehoe - Widener Sun Network Manager - brendan@cs.widener.edu
  Widener University in Chester, PA                A Bloody Sun-Dec War Zone
 Now that we know he has ID, we could give him an account. finger bush@cs....

c60b-1eq@web-1b.berkeley.edu (Noam Mendelson) (03/20/91)

In article <1214@caslon.cs.arizona.edu> armstron@cs.arizona.edu (Jim Armstrong) writes:
>
>How can I get atof() to return a float instead of a double? 
>When I run this simple program I get n = 37.549999 instead of
>n = 37.55.  What am I doing wrong?
>
>
>#include <stdio.h>
>
>main()
>{
>
>double atof();
>char buf[10];
>float n;
>
>strcpy(buf,"37.55");
>n = (float) atof(buf);
>printf("n = %f\n",n);
>
>}

atof() _is_ returning a float.  It is a different matter if you would
like to round it off to two decimal places.  In that case you might
want to try floor(x*100)/100.

===============================================================
Noam Mendelson                       | "I haven't lost my mind,
c60b-1eq@web.Berkeley.EDU            |  it's backed up on tape
University of California at Berkeley |  somewhere."

torek@elf.ee.lbl.gov (Chris Torek) (03/20/91)

>In <1214@caslon.cs.arizona.edu>, armstron@cs.arizona.edu writes:
>>When I run this simple program I get n = 37.549999 instead of
>>n = 37.55.  What am I doing wrong?

Welcome to the Wacky World of Floating Foint, I mean Point!

In article <1991Mar19.181719.7821@cs.widener.edu> brendan@cs.widener.edu
(Brendan Kehoe) writes:
>  I have a feeling it's got something to do with the way printf takes
>  its argument .. notice if ya do
>	printf("n = %.2f\n", n);
>  you do get 37.55.

The problem is that the number 37.55 does not exist.  There are two
numbers that are pretty close to 37.55, both of which are `about'
1.1734375 x 32 (the number you want), but one is just a bit under,
and the other just a bit over.  Your runtime system picks the lower
value, and when you print it out it says `37.549999'.

Using %.2f tells printf to `round to two digits after the decimal point'.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

gwyn@smoke.brl.mil (Doug Gwyn) (03/21/91)

In article <1214@caslon.cs.arizona.edu> armstron@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.

dave@cs.arizona.edu (Dave Schaumann) (03/21/91)

In article <15522@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <1214@caslon.cs.arizona.edu> armstron@cs.arizona.edu (Jim Armstrong) writes:
>>When I run this simple program I get n = 37.549999 instead of
>>n = 37.55.  What am I doing wrong?
>
>[...]
>I think your real problem is that you think that the value 37.55 can
>be represented exactly in a binary floating-point representation.

Reminds me of a quote I heard or read somewhere.  (sorry, can't remember,
but feel free to enlighten me if you know...)

	Using floating point arithmetic is like moving piles of sand.
	Every time you do, you lose a little sand and pick up a little dirt.

-- 
Dave Schaumann | dave@cs.arizona.edu | Short .sig's rule!

xwang@gmuvax2.gmu.edu (Xiang-Min Wang) (03/24/91)

In article <1214@caslon.cs.arizona.edu> armstron@cs.arizona.edu (Jim Armstrong) writes:
>
>How can I get atof() to return a float instead of a double? 
>When I run this simple program I get n = 37.549999 instead of
>n = 37.55.  What am I doing wrong?
>

I think the error is caused by the physical machine. It is nothing to do
with the float-type conversion. Also, I think you did what you needed to
do.

>Jim Armstrong			  "The nonpayment and subsequent abuse of

xwang