www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/18/12:31:40

From: rpolzer AT www42 DOT t-offline DOT de (echo 'Rudolf Polzer'>/dev/null)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Atof
References: <3ADDDAE3 DOT 29499 DOT 112013C AT localhost>
X-newsgroup: comp.os.msdos.djgpp
X-realname: Igor Bujna
X-Ringtones: http://ringtones AT durchnull DOT de
X-Original: no
Message-ID: <slrn9drh7f.85g.rpolzer@www42.t-offline.de>
User-Agent: slrn/0.9.6.3 (Linux)
Date: Wed, 18 Apr 2001 18:46:40 +0200
Lines: 51
NNTP-Posting-Host: 213.7.25.186
X-Trace: 987611387 news.freenet.de 7880 213.7.25.186
X-Complaints-To: abuse AT freenet DOT de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Igor Bujna <igor DOT bujna AT maxi-tip DOT cz> 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.

The problem is that floats, doubles and long doubles are stored in 
binary form and -21.345 does not have a binary representation.

Look at this:

0.1 (decimal)
=
1 / 10
= 1 / 1010b = 0.000110011001100110...
  0
  =
  10
   0
  ==
  100
    0
  ===
  1000
  1000
  ====
  10000
   1010
   ====
    1100
    1010
    ====
      100 etc.


Try using double or long double, and if that is not enough, round:
sprintf ("%.4f", a) or sth like that
      

-- 
#!/usr/bin/perl
eval($0=q{$0="\neval(\$0=q{$0});\n";for(<*.pl>){open X,">>$_";print X
$0;close X;}print''.reverse"\nsuriv lreP trohs rehtona tsuJ>RH<\n"});
####################### http://learn.to/quote #######################

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019