Board index » delphi » Interesting problem wirh ROUND or FLOATTOSTR?????

Interesting problem wirh ROUND or FLOATTOSTR?????

Create a form with 3 edit boxes and a button. Edit1 = float such as
12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
= result of this button click.

procedure TForm1.Button1Click(Sender: TObject);
var
   t1, t2, t3: Single;
begin
   t1 := StrToFloat(Edit1.Text);
   t2 := StrToInt(Edit2.Text);
   t2 := exp(t2*ln(10));
   t3 := t1*t2;
   t3 := Round(t3);
   t3 :=t3/t2;
   Edit3.Text := FloatToStr(t3);
end;

edit3 reads the following on my PC:  12.3459997177124

i.e. Round doesnt work or FloattoStr has floating point math error.

Any Comments????

Eric

 

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
wrote:

Actually, the problem is not with either function but when I divide
12345 by 1000 I get 12.3459997177124.  How do I get around this
problem?

Eric

Quote
>Create a form with 3 edit boxes and a button. Edit1 = float such as
>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>= result of this button click.

>procedure TForm1.Button1Click(Sender: TObject);
>var
>   t1, t2, t3: Single;
>begin
>   t1 := StrToFloat(Edit1.Text);
>   t2 := StrToInt(Edit2.Text);
>   t2 := exp(t2*ln(10));
>   t3 := t1*t2;
>   t3 := Round(t3);
>   t3 :=t3/t2;
>   Edit3.Text := FloatToStr(t3);
>end;

>edit3 reads the following on my PC:  12.3459997177124

>i.e. Round doesnt work or FloattoStr has floating point math error.

>Any Comments????

>Eric

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
wrote:

Quote
>Create a form with 3 edit boxes and a button. Edit1 = float such as
>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>= result of this button click.

>procedure TForm1.Button1Click(Sender: TObject);
>var
>   t1, t2, t3: Single;
 ...
>edit3 reads the following on my PC:  12.3459997177124

>i.e. Round doesnt work or FloattoStr has floating point math error.

What's the problem?  Single precision is only good to 6 or 7 digits,
and the result you got is correct to 6 or 7 digits.  If you want more
precision, use a bigger floating point type (i.e. double or extended).

Duncan Murdoch

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


On Mon, 27 May 1996 20:47:48 GMT, dmurd...@mast.queensu.ca (Duncan

Quote
Murdoch) wrote:
>On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
>wrote:

>>Create a form with 3 edit boxes and a button. Edit1 = float such as
>>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>>= result of this button click.

>>procedure TForm1.Button1Click(Sender: TObject);
>>var
>>   t1, t2, t3: Single;
> ...
>>edit3 reads the following on my PC:  12.3459997177124

>>i.e. Round doesnt work or FloattoStr has floating point math error.

>What's the problem?  Single precision is only good to 6 or 7 digits,
>and the result you got is correct to 6 or 7 digits.  If you want more
>precision, use a bigger floating point type (i.e. double or extended).

>Duncan Murdoch

It's not correct to 6 or 7 digits.  Its not even correct to 3 digits.
The result should be 12.346 even.  I dont want more precision. I am
trying to round the numbers.  It is not Round or FloatToStr causing
the problem.  The problem is when I divide 12346 by 1000 I get
12.3459997177124.  And I dont even have a Pentium!!! :)

Any clues?

Eric

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


Quote
G. Eric Morgan wrote:
> It's not correct to 6 or 7 digits.  Its not even correct to 3 digits.
> The result should be 12.346 even.  I dont want more precision. I am
> trying to round the numbers.  It is not Round or FloatToStr causing
> the problem.  The problem is when I divide 12346 by 1000 I get
> 12.3459997177124.  And I dont even have a Pentium!!! :)

Well, 6 correct digits means that the error is less than 1 in 1000000.

12.346-12.3459997177124=0.00000029 (approx), which is less than 1 in
1000000 (actually 0.00000029/12.34=2e-8). This does NOT mean that the 6
first digits are correct.

--
Svante Granqvist        Speech, Music and Hearing
Phone +46-8-790 7561    Box 700 14
Fax   +46-8-790 7854    S-100 44 Stockholm
sva...@speech.kth.se    http://www.speech.kth.se/~svante

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


morg...@primenet.com (G. Eric Morgan) wrote:

Quote
>Create a form with 3 edit boxes and a button. Edit1 = float such as
>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>= result of this button click.

>procedure TForm1.Button1Click(Sender: TObject);
>var
>   t1, t2, t3: Single;
>begin
>   t1 := StrToFloat(Edit1.Text);
>   t2 := StrToInt(Edit2.Text);
>   t2 := exp(t2*ln(10));
>   t3 := t1*t2;
>   t3 := Round(t3);
>   t3 :=t3/t2;
>   Edit3.Text := FloatToStr(t3);
>end;

>edit3 reads the following on my PC:  12.3459997177124

>i.e. Round doesnt work or FloattoStr has floating point math error.

>Any Comments????

Yeah,

Change your Single's to Extended and it'll work fine. I've had never
ending problems with this - Delphi use's Extended for all floating
point calculations, so if you use something else ( eg. Singles or
Doubles ) then you get weird rounding problems as it converts your
values back and forth. The only way I've got around this is to always
use the Extended type.

Hope that helps,

Simon.

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


On 27 May 1996 18:39:02 -0700, morg...@primenet.com (G. Eric Morgan) wrote:

Quote
>On Mon, 27 May 1996 20:47:48 GMT, dmurd...@mast.queensu.ca (Duncan
>Murdoch) wrote:

>>On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
>>wrote:

>>>Create a form with 3 edit boxes and a button. Edit1 = float such as
>>>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>>>= result of this button click.

>>>procedure TForm1.Button1Click(Sender: TObject);
>>>var
>>>   t1, t2, t3: Single;
>> ...
>>>edit3 reads the following on my PC:  12.3459997177124

>>>i.e. Round doesnt work or FloattoStr has floating point math error.

>>What's the problem?  Single precision is only good to 6 or 7 digits,
>>and the result you got is correct to 6 or 7 digits.  If you want more
>>precision, use a bigger floating point type (i.e. double or extended).

>>Duncan Murdoch

>It's not correct to 6 or 7 digits.  Its not even correct to 3 digits.
>The result should be 12.346 even.  I dont want more precision. I am
>trying to round the numbers.  It is not Round or FloatToStr causing
>the problem.  The problem is when I divide 12346 by 1000 I get
>12.3459997177124.  And I dont even have a Pentium!!! :)

>Any clues?

>Eric

Certain decimal numbers, and 12.346 must be one of them, cannot be represented
accurately in base 2. That is most likely the source of the "error." You should
be able to get the original number by rounding to 3 decimal positions. The only
way to avoid such situations is to use fixed point arithmetic rather than
floating point.

Components are (or should be) available that can do fixed point arithmetic to
almost any precision. If you only need relatively small numbers within the range
of 15 or 31 bits then you can use normal binary numbers rather than float and
get accurate results. It is only when you need the capability of exceeding that
range that you need floating point. If you need longer numbers then you should
round to the required precision and you should get accurate results.

dick
{*word*106} MacDonald

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


Quote
>It's not correct to 6 or 7 digits.  Its not even correct to 3 digits.

??????? It's correct to 8 digits (round   12.3459997177124 to 8 significant
digits: you'll get   12.346000).

Quote
>The result should be 12.346 even.  I dont want more precision. I am
>trying to round the numbers.  It is not Round or FloatToStr causing
>the problem.  The problem is when I divide 12346 by 1000 I get
>12.3459997177124.  And I dont even have a Pentium!!! :)

The problem is that it's never gonna be 12.346 even: while 12.346 can
be represented exactly as a finite ratio in a decimal, it's an infinite
ratio in a binary notation (which is used by your processor, even if it's
not a faulty Pentium).

Use FloatToStrF(Number,ffGeneral,8,8) - or something like that
(not exactly sure about the parameters); and specify number of significant
digits in your converted string LESS than precision of the type you are
using.

Dmitry

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


Quote
G. Eric Morgan wrote:
> =
> On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
> wrote:
> =
> Actually, the problem is not with either function but when I divide
> 12345 by 1000 I get 12.3459997177124.  How do I get around this
> problem?
> =
> Eric
> =
> >Create a form with 3 edit boxes and a button. Edit1 =3D float such as
> >12.3456789.  Edit 2=3D number of decimals to round to such as 3.  Edit=
 3
> >=3D result of this button click.

> >procedure TForm1.Button1Click(Sender: TObject);
> >var
> >   t1, t2, t3: Single;
> >begin
> >   t1 :=3D StrToFloat(Edit1.Text);
> >   t2 :=3D StrToInt(Edit2.Text);
> >   t2 :=3D exp(t2*ln(10));
> >   t3 :=3D t1*t2;
> >   t3 :=3D Round(t3);
> >   t3 :=3Dt3/t2;
> >   Edit3.Text :=3D FloatToStr(t3);
> >end;

> >edit3 reads the following on my PC:  12.3459997177124

> >i.e. Round doesnt work or FloattoStr has floating point math error.

> >Any Comments????

> >Eric

The problem is not delphi...

Floating point numbers are represented in a BINARY form (not decimals)...=
 so =

a number that can be represented precisly with a by a power of 10 may not=
 be =

represented exactly with a power of 2...

This is an old problem with any application that need really precise =

result... The only overcome you can do is to use a larger precision type =
and =

then round it... Or is to build your own routine that do the calculation =
with =

a a decimal base ... The later will give a MUCH slower result...

Even Supercomputer have the problem ;-) And this can give headache to =

scientist who really need fast precise calculation in decimals...

There is an article in Wired (somewhere in the Autumn '95) that is talkin=
g =

about the implication of that fact...

Hope it help...

-- =

----------
Emmanuel Pirsch
Visitez la Chaumi=E8re Virtuelle : http://www.odyssee.net/~tasm
-------------------------------------------------------------
"Gravitation cannot be held responsible for people falling
in love."
        - Albert Einstein.

Re:Interesting problem wirh ROUND or FLOATTOSTR?????


On 27 May 1996 12:49:01 -0700, morg...@primenet.com (G. Eric Morgan)
wrote:

Quote
>Create a form with 3 edit boxes and a button. Edit1 = float such as
>12.3456789.  Edit 2= number of decimals to round to such as 3.  Edit 3
>= result of this button click.
...
>edit3 reads the following on my PC:  12.3459997177124

>i.e. Round doesnt work or FloattoStr has floating point math error.

There is no error in the code or in Delphi. Read the other threads
about inexactness and floating point errors.

Then look up the FloatToStrF function.
--
Ray Lischner                              li...@tempest-sw.com
Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

Other Threads