Board index » cppbuilder » FloatToStr() doesn't work properly

FloatToStr() doesn't work properly

Hi all.

I have a very simple question:
Why does FloatToString the following:

   float f = 1.2;
   ShowMessage(FloatToStr(f));

What you get is this...

" 1,20000004768372"

Is this a bug or a bad setting in my bcb5 german standard
oder where is my mistake?
Thank you for your help!

Mathias

 

Re:FloatToStr() doesn't work properly


Quote
> If you're objecting to the "4768372", remember... floating point is not
> exact.

Actually, floating point representation is exact, in a reciprocal power of
two times a coefficient.  However, the conversion from the floating point
format to decimal, or vice versa, may not be exact.

k

Re:FloatToStr() doesn't work properly


I shopuld have written:

...to the function FloatToStr()

Rodolfo

Quote
> So, the information about the 1.2 number provided by your float variable f
> to the function FloatToInt() is not enough to account for the 8th decimal
> digit onwards.

Re:FloatToStr() doesn't work properly


The FloatToStr() is defined as:

extern PACKAGE AnsiString __fastcall FloatToStr(Extended Value);

It takes and Extended parameter which is a long double in C++.
The function name is unfortunate, it should be called something like
ExtendedToStr() or LongDoubleToStr()

You are getting 1.20000004768... because the float data type has only
7-digits
of accuracy ("precision") while the long double data ty has 18 digits of
accuracy.
So, the information about the 1.2 number provided by your float variable f
to
 the function FloatToInt() is not enough to account for the 8th decimal
digit onwards.

   //float provides 7-digit precision
   float f = 1.2;

   //Extended  (long double)  provides 18-digit precision
   Extended     ex = 1.2;
   long double ld = 1.2;

   ShowMessage(FloatToStr(f));     //innaccurate after the 7th decimal place

   ShowMessage(FloatToStr(ex));   //accurate
   ShowMessage(FloatToStr(ld));   //accurate

Rodolfo

Quote
"Mathias Michaelis" <Maff...@gmx.de> wrote in message

news:av9jrh$jcp$02$1@news.t-online.com...
Quote
> Hi all.

> I have a very simple question:
> Why does FloatToString the following:

>    float f = 1.2;
>    ShowMessage(FloatToStr(f));

> What you get is this...

> " 1,20000004768372"

> Is this a bug or a bad setting in my bcb5 german standard
> oder where is my mistake?
> Thank you for your help!

> Mathias

Re:FloatToStr() doesn't work properly


Let's get real picky. <grin>

As long as the number is a sum of powers of 2 (positive, negative, or zero)
time integral coefficients, where the resultant contains no more mantissa
bits than the mantissa of the type used, then the representation is exact.
(E.g., 3/8 + 13/32 is exact).

Quote
"Keith" <kdop...@sbcglobal.net> wrote in message

news:3E18B49A.4B24AB2@sbcglobal.net...
Quote
> > If you're objecting to the "4768372", remember... floating point is not
> > exact.

> Actually, floating point representation is exact, in a reciprocal power of
> two times a coefficient.  However, the conversion from the floating point
> format to decimal, or vice versa, may not be exact.

> k

Re:FloatToStr() doesn't work properly


<g>

Re:FloatToStr() doesn't work properly


Oh, thank you very much.
This makkes is clear.

I didn't expect this interesting discussion...:)

Mathias

Re:FloatToStr() doesn't work properly


We didn't expect such an intersting question...

Rodolfo

Quote
"Mathias Michaelis" <Maff...@gmx.de> wrote in message

news:avbl1r$hqm$03$1@news.t-online.com...
Quote
> Oh, thank you very much.
> This makkes is clear.

> I didn't expect this interesting discussion...:)

Re:FloatToStr() doesn't work properly


"Mathias Michaelis" <Maff...@gmx.de> escreveu na mensagem
news:av9jrh$jcp$02$1@news.t-online.com...

Quote
> Hi all.

> I have a very simple question:
> Why does FloatToString the following:

>    float f = 1.2;
>    ShowMessage(FloatToStr(f));

> What you get is this...

> " 1,20000004768372"

> Is this a bug or a bad setting in my bcb5 german standard
> oder where is my mistake?
> Thank you for your help!

> Mathias

You can allways format your data with FormatFloat!!
ex..: FormatFloat("###,###,##0.00", number );

Other Threads