From: Henrique on
Hi,

I have some problems in converting float variables in Ada. Look at the
code below.

Why var4 is not equal var1?

Thanks.

--
Execution results:
--

var3: =
var4: >

--
Code:
--

with Ada.Text_IO, Ada.Float_Text_IO;
use Ada.Text_IO, Ada.Float_Text_IO;

procedure Test is

type My_Float is digits 6;

CONVERSION_CONSTANT : constant My_Float := 6076.11;
CONVERSION_CONSTANT2 : constant My_Float := 1.0 /
CONVERSION_CONSTANT;

package Float_Text_IO is new Ada.Text_IO.Float_IO (Long_Float);
use Float_Text_IO;

var1: My_Float := 999.9;
var2: My_Float := var1*CONVERSION_CONSTANT;
var3: My_Float := var2/CONVERSION_CONSTANT;
var4: My_Float := var2*CONVERSION_CONSTANT2;
begin
Put("var3: ");
if var3 = var1 then
Put("=");
elsif var3 > var1 then
Put(">");
else
Put("<");
end if;

New_Line;
Put("var4: ");
if var4 = var1 then
Put("=");
elsif var4 > var1 then
Put(">");
else
Put("<");
end if;
end Test;
From: Jacob Sparre Andersen on
Henrique <henriquemagalhaes(a)gmail.com> wrote:

> Why var4 is not equal var1?

> type My_Float is digits 6;
>
> CONVERSION_CONSTANT : constant My_Float := 6076.11;
> CONVERSION_CONSTANT2 : constant My_Float := 1.0 / CONVERSION_CONSTANT;
>
> var3: My_Float := var2/CONVERSION_CONSTANT;
> var4: My_Float := var2*CONVERSION_CONSTANT2;

Basically because the relative precision of My_Float is limited to 1e-6.

In general "=" does not make much sense for floating point types.

Cheers,

Jacob
--
Jacob Sparre Andersen Research & Innovation
Vesterbrogade 148K, 1. th.
1620 K�benhavn V
Danmark

Phone: +45 21 49 08 04
E-mail: jacob(a)jacob-sparre.dk
Web-sted: http://www.jacob-sparre.dk/
From: Dmitry A. Kazakov on
On Thu, 29 Jul 2010 05:23:42 -0700 (PDT), Henrique wrote:

> I have some problems in converting float variables in Ada. Look at the
> code below.
>
> Why var4 is not equal var1?

Because they are not. Floating-point operations are inexact.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Georg Bauhaus on
On 29.07.10 14:23, Henrique wrote:

> package Float_Text_IO is new Ada.Text_IO.Float_IO (Long_Float);

(Any reason you didn't instantiate with My_Float?)


For illustration, inserting Put(Var3, Aft => 15) and
Put(Var4, Aft => 15), I get

$ ./test_fpt
var3: 9.999000244140625E+02=
var4: 9.999000854492188E+02>

Choosing 15 digits instead of 6 in the type definition, I get

$ ./test_fpt
var3: 9.999000000000000E+02=
var4: 9.999000000000001E+02>

There is always a FPT computation that isn't giving the exact
result, since there aren't enough bits for doing real math on
any computer, if you try hard enough.

Georg
From: Georg Bauhaus on
On 29.07.10 14:46, Dmitry A. Kazakov wrote:
> On Thu, 29 Jul 2010 05:23:42 -0700 (PDT), Henrique wrote:
>
>> I have some problems in converting float variables in Ada. Look at the
>> code below.
>>
>> Why var4 is not equal var1?
>
> Because they are not. Floating-point operations are inexact.
>

Interestingly, a quick rewrite to C translated with gcc gives a different
impression (of equality),

#include <stdio.h>

int main()
{
typedef float My_Float;


#define CONVERSION_CONSTANT 6076.11
const My_Float CONVERSION_CONSTANT2 = 1.0 / CONVERSION_CONSTANT;


My_Float Var1 = 999.9;
My_Float Var2 = Var1*CONVERSION_CONSTANT;
My_Float Var3 = Var2/CONVERSION_CONSTANT;
My_Float Var4 = Var2*CONVERSION_CONSTANT2;

fputs("var3: ", stdout); printf("%.15f", Var3);
if (Var3 = Var1)
fputs("=", stdout);
else if (Var3 > Var1)
fputs(">", stdout);
else
fputs("<", stdout);

fputs("\n", stdout);
fputs("var4: ", stdout); printf("%.15f", Var4);
if (Var4 = Var1)
fputs("=", stdout);
else if (Var4 > Var1)
fputs(">", stdout);
else
fputs("<", stdout);


fputs("\n", stdout);
return 0;
}


$ ./a.out
var3: 999.900024414062500=
var4: 999.900085449218750=
 |  Next  |  Last
Pages: 1 2 3 4 5 6 7
Prev: What is your preferred VCS?
Next: GPS 2010 for AVR