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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,6a8952cbe009f3ed X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.66.81.225 with SMTP id d1mr1144326pay.25.1360469025418; Sat, 09 Feb 2013 20:03:45 -0800 (PST) MIME-Version: 1.0 Path: mj10ni887pbb.1!nntp.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!nrc-news.nrc.ca!goblin3!goblin.stu.neva.ru!nntp-feed.chiark.greenend.org.uk!ewrotcd!reality.xs3.de!news.jacob-sparre.dk!munin.jacob-sparre.dk!pnx.dk!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Numerical calculations: Why not use fixed point types for everything? Date: Mon, 4 Feb 2013 20:27:58 -0600 Organization: Jacob Sparre Andersen Research & Innovation Message-ID: References: <4905b963-0036-4129-8050-fb26ef0154d6@googlegroups.com> <32314026-23ae-45b8-a4c5-e589e7d79de2@googlegroups.com> <64e3c342-d042-40a2-8a16-b1f0cdff9f16@googlegroups.com> <91527f7c-0679-4c21-95c7-a07f3fff265d@googlegroups.com> NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: munin.nbi.dk 1360031280 11272 69.95.181.76 (5 Feb 2013 02:28:00 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Tue, 5 Feb 2013 02:28:00 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-Received-Bytes: 5304 Date: 2013-02-04T20:27:58-06:00 List-Id: "Ada novice" wrote in message news:c81e7d23-8757-4a81-9ca2-d540afe0d819@googlegroups.com... On Saturday, February 2, 2013 10:08:50 PM UTC+1, Nasser M. Abbasi wrote: >> >> Well, it is common only in sloppy programming. >> >> >> >> loop counters should always be discrete. as in integers, >I do not think we should judge things too quickly. I tend to agree with you in general, but your example seems to demostrate the OPs point... >By having a "delta" that we can explicitly specify, this can make the code >simpler to understand. Look at this code: > >with Ada.Text_IO; use Ada.Text_IO; >with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; > >procedure Step_Freq is > Count : Integer; > Frequency : Long_Float; > Frequency_Start : constant := -44.88; > Frequency_Step : constant := (67.32 - Frequency_Start) / 299.0; This 299 seems related to the one in the loop below. As such, I think it should have a named constant: Steps : constant := 300; Frequency_Step : constant := (67.32 - Frequency_Start) / (Steps-1); [Aside: This is legal even though the denominator is integer because of the extra operators defined in 4.5.5(17). And examples like this is why.] > Delta_Frequency : Long_Float; > > begin > Count := 0; > Frequency := Frequency_Start; > for I in 1..300 loop for I in 0 .. Step-1 loop -- This avoids the odd "-1" in the loop below. If you had directly used I somewhere -- else, this might not work, but it does in this example. > Frequency := Frequency_Start + Frequency_Step * Long_Float (I - 1); Frequency := Frequency_Start + Frequency_Step * Long_Float (I); > if I = 2 then if I = 1 then > Delta_Frequency := abs(Frequency - Frequency_Start); > Put (Item => Delta_Frequency, Fore => 3, Aft => 5, Exp => 0); > Put(" "); > Put (Item => Frequency_Step, Fore => 3, Aft => 5, Exp => 0); > New_Line; > end if; > Count := Count + 1; --?? Besides the fact that this isn't even used in the loop, it has the same value as I -- (or I-1 in the original code). Why duplicate the value? One the huge benefits of -- Ada is that you don't have to start every range at 0 or 1 - do whatever your -- program needs. > end loop; > Put_Line ("Count after loop is" & Integer'Image (Count)); -- Unless your program is *really* buggy, this is just the constant Step. > New_Line; > Put (Item => Frequency, Fore => 3, Aft => 3, Exp => 0); >end Step_Freq; > >Well, I am using a discrete loop counter. But is this the best way to go >about for such >a simple example? What if I want the Frequency_Step to take a specific >value? Then >I will have perhaps to play with the range and the amount of discrete >points I need in >order to arrive at a desired Frequency_Step in the code. Nah, all you need to do is make the Step value calculated from the Frequency_Step, rather than the reverse. >So why not use fixed-point for the frequency as we know exactly what step >we want? The main reason is that you almost always need to get your data from an array somewhere, and you use the for loop parameter ("I" in this case) to get the values from the array, and then use the float version of the step for the calculation. With the exception of benchmarks, I don't think I've ever written a loop that does exactly the same thing 300 times without changing the input data. (When such things appear to happen, usually you can refactor the expression to avoid the loop altogether, which is usually 300 times faster.) Randy.