Re: Float to string and vice versa

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Michael K. O'Neill (mikeathon2000_at_nospam.hotmail.com)
Date: 09/22/04


Date: Wed, 22 Sep 2004 08:05:51 -0700

There are an infinite number of values between .9999 and .99999 and with
float or double, there are only a finite number of choices to represent them
all.

So, you can never the precision with which numbers get represented.
Sometimes it's very good, sometimes it's not. A test like "if ( testedValue
== targetValue )..." is almost guaranteed to fail with floats or doubles.
Instead, you must do testing on acceptable error, such as "if ( abs(
testedValue-targetValue) <= .000001 )..." (or some other small number).

Mike

"Raed Sawalha" <raed_sawalha@hotmail.com> wrote in message
news:u2cMEqHoEHA.2340@TK2MSFTNGP10.phx.gbl...
> ok when i do this
> CString strComfortValue ;
>
> strComfortValue = objXml->GetChildData();
>
> //here strComfortValue = 0.9999
>
> float fComfortValue = atof(strComfortValue.GetString());
>
> //But the problem fComfortValue = 0.99989998
>
> WHY ? I need to get the same result of strComfortValue
>
>
>
> Regards
>
>
>
> "Sigurd Stenersen" <sigurds@utvikling.com> wrote in message
> news:u5PAnSHoEHA.868@TK2MSFTNGP10.phx.gbl...
> > Raed Sawalha wrote:
> > > Dear:
> > > Can I convert Float to string
> >
> > _ecvt(), _fcvt(), _gcvt() or sprintf()
> >
> > > and string to float like in integert
> > > (itoa)?
> >
> > atof() or sscanf()
> >
> >
> > --
> >
> >
> > Sigurd
> > http://utvikling.com
> >
> >
>
>



Relevant Pages

  • Re: Random numbers between -2.0 and +3.0
    ... Think of it -2.0 to 3.0 has a infinite number of values. ... This WOULD kiss the limits. ... I was going to respond the same except of course that the float are ... than the real number probability. ...
    (comp.lang.lisp)
  • Re: Float comparison
    ... <more snip> ... You seem to believe that the float therefore represents all those ... an unsigned int also represents an infinite number ...
    (comp.lang.c)
  • RE: REAL/DOUBLE PRECISION
    ... Real and float are approximate data types (not all vaules can be represented ... that behind SQL-Server Float is an IEEE Float representation. ... > Kind Regards ... > Justus Gadient ...
    (microsoft.public.sqlserver.programming)
  • Convert from unsigned char array to float!
    ... I need to convert from a unsigned char array to a float. ... Linuxon a Intel pentium machine. ... Regards, ...
    (comp.os.linux.development.apps)
  • Re: float variables in loops
    ... >> What happens to float variable in loops. ... > stored with a finite number of digits. ... > have an infinite number of digits (like one third is an infinite ...
    (comp.lang.c)