Message-ID: <3ADE8970.6366CCAE@earthlink.net> From: Martin Ambuhl X-Mailer: Mozilla 4.76 [en] (Win95; U) X-Accept-Language: en,zh-CN,fr,de-CH,ru MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Atof References: <3ADDDAE3 DOT 29499 DOT 112013C AT localhost> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 50 Date: Wed, 18 Apr 2001 18:43:59 GMT NNTP-Posting-Host: 209.246.65.189 X-Complaints-To: abuse AT earthlink DOT net X-Trace: newsread2.prod.itd.earthlink.net 987619439 209.246.65.189 (Wed, 18 Apr 2001 11:43:59 PDT) NNTP-Posting-Date: Wed, 18 Apr 2001 11:43:59 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net X-Received-Date: Wed, 18 Apr 2001 11:42:36 PDT (newsmaster1.prod.itd.earthlink.net) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Igor Bujna wrote: > > Hi, > i have this problem with atof(); > > char *buf = "-21.345\0"; > float f = 0; > > f = atof ( buf ); > printf("%f",f); > > , then i have this "-21.344999". > What i must to do for it will be OK. You don't have a problem with atof(). You have a problem with trying to get 8 digits of significance from a type only guaranteed 6 digits of significance: #include #include #include int main(void) { const char *buf = "-21.345\0"; float f = 0; double d = 0; f = atof(buf); printf("unadorned %%f applied to float: %f\n", f); printf("%%.3f applied to float: %.3f\n", f); printf("%%.*f (for FLT_DIG-2) applied to float: %.*f\n\n", FLT_DIG - 2, f); d = atof(buf); printf("unadorned %%f applied to double: %f\n", d); printf("%%.3f applied to double: %.3f\n", d); printf("%%.*f (for DBL_DIG-2) applied to double: %.*f\n\n", DBL_DIG - 2, d); return 0; } unadorned %f applied to float: -21.344999 %.3f applied to float: -21.345 %.*f (for FLT_DIG-2) applied to float: -21.3450 unadorned %f applied to double: -21.345000 %.3f applied to double: -21.345 %.*f (for DBL_DIG-2) applied to double: -21.3450000000000