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,FREEMAIL_FROM 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.224.178.207 with SMTP id bn15mr941063qab.1.1360134703449; Tue, 05 Feb 2013 23:11:43 -0800 (PST) X-Received: by 10.49.34.135 with SMTP id z7mr2303587qei.1.1360134703429; Tue, 05 Feb 2013 23:11:43 -0800 (PST) Path: k2ni8440qap.0!nntp.google.com!p13no14750686qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 5 Feb 2013 23:11:43 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=130.240.232.139; posting-account=Rr9I-QoAAACS-nOzpA-mGxtAlZ46Nb6I NNTP-Posting-Host: 130.240.232.139 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> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <4b654b06-f2d2-4ced-8508-c10e5c84e29d@googlegroups.com> Subject: Re: Numerical calculations: Why not use fixed point types for everything? From: Ada novice Injection-Date: Wed, 06 Feb 2013 07:11:43 +0000 Content-Type: text/plain; charset=ISO-8859-1 Date: 2013-02-05T23:11:43-08:00 List-Id: On Tuesday, February 5, 2013 3:27:58 AM UTC+1, Randy Brukardt wrote: > > >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 > Yes this is correct. Your code is a good alternative of mine. > > 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? Well I am just outputting the index counter I at the end which is 300. How do you get to output of the end value of the index counter otherwise? > > Put_Line ("Count after loop is" & Integer'Image (Count)); > > -- Unless your program is *really* buggy, this is just the constant Yes I know. See my answer above. > > 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.) > In this example, Frequency is changing at every index in the loop. Its first value is -44.88 and its final value is 67.32. This Frequency can be used further in a calculation. If you would have read the code well, you would have seen that the loop is not doing 300 times exactly the same thing. Thanks YC