/ Forside / Teknologi / Udvikling / C/C++ / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
C/C++
#NavnPoint
BertelBra.. 2425
pmbruun 695
Master_of.. 501
jdjespers.. 500
kyllekylle 500
Bech_bb 500
scootergr.. 300
gibson 300
molokyle 287
10  strarup 270
Problems with atof(). Urgent please!!
Fra : Javier Riera


Dato : 30-08-01 18:54

Problems with atof(). Urgent please!!

Hi all,

I found some strange behaviour while doing a cast from char(22) to float.

myFloat float;
myChar char(22);
.....
/* Mychar equals to "656300029111111001" */
myFloat=atof(myChar):
/* myFloat equals to 656300029111111040 ???!!!??? */
....

Input "656300029111111001" ... output float:656300029111111040.

Maybe there is something left in the compiler options... I really do not
know.

Please help me, 'cos this error is going to bring me a great problem....


Thanks in advance,

Javier Riera



 
 
Frank Hahn (30-08-2001)
Kommentar
Fra : Frank Hahn


Dato : 30-08-01 20:30

"Javier Riera" <javier_riera@terra.es> wrote in message
news:9mluh5$el1$1@news.wanadoo.es...
> Problems with atof(). Urgent please!!
>
> Hi all,
>
> I found some strange behaviour while doing a cast from char(22) to
float.
>
> myFloat float;
> myChar char(22);
> ....
> /* Mychar equals to "656300029111111001" */
> myFloat=atof(myChar):
> /* myFloat equals to 656300029111111040 ???!!!??? */
> ...
>
> Input "656300029111111001" ... output float:656300029111111040.
>
> Maybe there is something left in the compiler options... I really do not
> know.
>
> Please help me, 'cos this error is going to bring me a great problem....

It's not a problem, or rather, it's not an error.
It's a result of the limited "resolution" (precision) of the float-datatype.

The float xxx040 is just the closest representation of the number, which can
be contained
in the float-datatype.

/Frank




Ivan Johansen (31-08-2001)
Kommentar
Fra : Ivan Johansen


Dato : 31-08-01 17:19

Frank Hahn wrote:

> It's not a problem, or rather, it's not an error.
> It's a result of the limited "resolution" (precision) of the float-datatype.
>
> The float xxx040 is just the closest representation of the number, which can
> be contained
> in the float-datatype.


And the solution is to use a type with a better resolution, for example
double or long double.

Ivan Johansen


Frank Hahn (31-08-2001)
Kommentar
Fra : Frank Hahn


Dato : 31-08-01 19:33

"Ivan Johansen" <NG@Padowan.dk> wrote in message
news:3B8FB8E6.9030800@Padowan.dk...
> Frank Hahn wrote:
>
> > It's not a problem, or rather, it's not an error.
> > It's a result of the limited "resolution" (precision) of the
float-datatype.
> >
> > The float xxx040 is just the closest representation of the number, which
can
> > be contained
> > in the float-datatype.
>
>
> And the solution is to use a type with a better resolution, for example
> double or long double.

I guess atof(..) sets the limit.
According to "man atof" the default return-type is a double.
So long double would be overkill.

/Frank




Ivan Johansen (01-09-2001)
Kommentar
Fra : Ivan Johansen


Dato : 01-09-01 10:10

Frank Hahn wrote:

> I guess atof(..) sets the limit.
> According to "man atof" the default return-type is a double.
> So long double would be overkill.

Borland has a _atold() that returns a long double, but I don't think
this is suported by all compilers.

You could also use sscanf(). This example converts to a long double:

long double myFloat;
char myChar[] = "656300029111111001";
sscanf(myChar, "%Lf",&myFloat);

Ivan Johansen


Tor Rustad (02-09-2001)
Kommentar
Fra : Tor Rustad


Dato : 02-09-01 18:13

"Frank Hahn" <efgehoNO@SPAMcs.auc.dk> wrote in message
> "Ivan Johansen" <NG@Padowan.dk> wrote in message
> >
> >
> > And the solution is to use a type with a better resolution, for example
> > double or long double.
>
> I guess atof(..) sets the limit.
> According to "man atof" the default return-type is a double.
> So long double would be overkill.

Right, atof() returns a double. However, atof() is not really a recommended
function to use, due to undefined behavoir in case the conversion fails.
strtod() is a more robust and better function to use for this.

In C99, strtold() has been added, and this function returns a long double.

--
Tor <torust AT online DOT no>



Søg
Reklame
Statistik
Spørgsmål : 177552
Tips : 31968
Nyheder : 719565
Indlæg : 6408847
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste