From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9b4538cfeb0c3576 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!volia.net!news2.volia.net!feed-A.news.volia.net!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Float conversion Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <9e669a3b-1013-4bd1-b372-5f7dfa46d083@f42g2000yqn.googlegroups.com> <1q5zc0ais535h$.1jqwfxhj9cflc$.dlg@40tude.net> <4c519968$0$6893$9b4e6d93@newsspool2.arcor-online.net> Date: Thu, 29 Jul 2010 17:35:05 +0200 Message-ID: <1d1txn4x3r5xn.1trm4gx9n87gm$.dlg@40tude.net> NNTP-Posting-Date: 29 Jul 2010 17:35:03 CEST NNTP-Posting-Host: 83591e35.newsspool2.arcor-online.net X-Trace: DXC=@UDUL 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