Prev: What is your preferred VCS?
Next: GPS 2010 for AVR
From: Georg Bauhaus on 29 Jul 2010 11:10 On 29.07.10 17:08, Georg Bauhaus wrote: > if (Var3 = Var1) Ouuuch!
From: Dmitry A. Kazakov on 29 Jul 2010 11:35 On Thu, 29 Jul 2010 17:08:24 +0200, Georg Bauhaus wrote: > 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), > [...] > > $ ./a.out > var3: 999.900024414062500= > var4: 999.900085449218750= with Ada.Text_IO; use Ada.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; 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_Line ("var1" & Long_Float'Image (Long_Float (Var1))); Put_Line ("var4" & Long_Float'Image (Long_Float (Var4))); end Test; ------------------------ var1 9.99900024414063E+02 var4 9.99900085449219E+02 When rounded to 6 decimal digits both are same. But the underlying base binary type is longer than 6 digits. P.S. It is always useful to think of floating point numbers as intervals (which they are) rather than numbers. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de
From: Warren on 29 Jul 2010 11:37 Dmitry A. Kazakov expounded in news:1q5zc0ais535h$.1jqwfxhj9cflc$.dlg@ 40tude.net: > 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. I.e. "... inexact for _all_ numbers". Some fractions do express themselves exactly in binary (like 0.5). That is why it may "seem" to work some of the time ;-) Warren
From: Jeffrey R. Carter on 29 Jul 2010 13:56 On 07/29/2010 07:56 AM, Georg Bauhaus wrote: > 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?) Since it's not used, it doesn't matter what it's instantiated with. -- Jeff Carter "Drown in a vat of whiskey. Death, where is thy sting?" Never Give a Sucker an Even Break 106
From: Henrique on 29 Jul 2010 14:21
On Jul 29, 12:35 pm, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de> wrote: > ------------------------ > var1 9.99900024414063E+02 > var4 9.99900085449219E+02 > > When rounded to 6 decimal digits both are same. But the underlying base > binary type is longer than 6 digits. > > P.S. It is always useful to think of floating point numbers as intervals > (which they are) rather than numbers. > > -- > Regards, > Dmitry A. Kazakovhttp://www.dmitry-kazakov.de As I declared the type with 6 digits, I expected that it would make the comparison only for these digits (this would gave var4 = var1). Do I always need to manually truncate the float number to the number of desired digits before making a comparison of them?? So what is the advantage of declaring it as "digits 6"? Thanks |