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,63043e3a9a3d050f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news1.google.com!news.glorb.com!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!news.weisnix.org!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: loop problem with exit condition 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: <3087f399-2cae-4bc8-bba1-e728943473bb@e14g2000yqe.googlegroups.com> Date: Sun, 26 Sep 2010 11:10:24 +0200 Message-ID: <1spdu1yb2khd0$.rk8xvebgn574.dlg@40tude.net> NNTP-Posting-Date: 26 Sep 2010 11:10:24 CEST NNTP-Posting-Host: 242843cd.newsspool2.arcor-online.net X-Trace: DXC=\4Obd:EYY?<_A0jCfgHO6>A9EHlD;3Yc24Fo<]lROoR18kF[ On Sun, 26 Sep 2010 01:16:37 -0700 (PDT), Ada novice wrote: > One small mistake: Inner range is divided into 100 steps (not 300) as > I wrote. But the number is irrelevant and the code should > work fine with all limits. You have accumulation of errors, which typical when floating-point numbers of same sign are summed. As the sum grows, addition looses more and more of its precision. There are several strategies how to handle this problem. But in your case the simplest way to remove bias would be to multiply the step: with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; procedure Try is Count : Integer; Outer : Long_Float; Outer_Start : constant := -44.88; Outer_Step : constant := (67.32 - Outer_Start) / 299.0; Inner : Long_Float; Inner_Start : constant := 0.001; Inner_Step : constant := (0.35 - Inner_Start) / 99.0; begin Count := 0; Outer := Outer_Start; for I in 1..300 loop Outer := Outer_Start + Outer_Step * Long_Float (I - 1); for J in 1..100 loop Count := Count + 1; Inner := Inner_Start + Inner_Step * Long_Float (J - 1); end loop; end loop; Put ("Inner after loop is (should be 0.35) "); Put (Item => Inner, Fore => 3, Aft => 3, Exp => 0); New_Line; Put_Line ("Count after loop is" & Integer'Image (Count)); end Try; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de