Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!caen!uwm.edu!lll-winken!ncis.tis.llnl.gov!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Simple atof() question Message-ID: <11115@dog.ee.lbl.gov> Date: 19 Mar 91 19:32:11 GMT References: <1214@caslon.cs.arizona.edu> <1991Mar19.181719.7821@cs.widener.edu> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Distribution: na Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 23 X-Local-Date: Tue, 19 Mar 91 11:32:12 PST >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